2017-01-06 8 views
0

私はNetwork.Wreq、Control.Lensを使用しています、Data.Aeson例外:JSONErrorアプリケーション/ random.v4 + JSON "" "レスポンスのコンテンツタイプがである" #Haskell

getInfo = do 
    let opts = defaults && header "Accept" .~ ["application/random.v4+json"] "Content-Type" .~ ["application/json"] 
    resp <- asJSON =<< getWith opts "url" :: IO (Response (Map String Value)) 
    let respBody = resp ^. responseBody 
    return respBody 

応答がContent-Type → application/random.v4+jsonを持っています

私は関数を呼び出すときに私が代わりにいくつかの空想を持っていることの応答シンプルContent-Type → application/jsonを持っているAPIへの呼び出しを行うとき、それは

Exception: JSONError "content type of response is \"application/random.v4+json\"" 

を言って例外をスローしかし、

私の機能はすべて期待どおりです。 contenty-typeapplication/json以外に変更されると、上記のエラーが表示されます。

なぜ例外がスローされますか?私のAPIのすべてのバージョンでどのように動作させることができますか?

**注:両方のバージョンのAPIでは、同じ種類のデータが返されます。

答えて

1

Network.Wreq.asJSONthrow a JSONError応答のコンテンツタイプが"application/json"と異なる場合。

私が行くための最も簡単な方法は、明示的にJSONとしてレスポンスボディをデコードするアイソーンを使用することです推測:

getInfo = do 
    let opts = defaults && header "Accept" .~ ["application/random.v4+json"] "Content-Type" .~ ["application/json"] 
    resp <- getWith opts "url" 
    let respBody = decodeResponseBody $ resp ^. responseBody 
    return respBody 
    where 
    decodeResponseBody :: ByteString -> Map String Value 
    decodeResponseBody body = 
     case eitherDecode' body of 
     Left err -> fail err 
     Right val -> return val 
関連する問題