2017-01-06 58 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

当我打电话的功能,它抛出一个异常说

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

但是,当我拨打电话到API,其响应具有简单Content-Type → application/json而不是有一些奇特Content-Type → application/random.v4+json

我的功能工作所有预期。当回应contenty-type更改为application/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 
相关问题