异常:JSONError "content type of response is \"application/random.v4+json\"" #Haskell
Exception: JSONError "content type of response is \"application/random.v4+json\"" #Haskell
我正在使用 Network.Wreq、Control.Lens、Data.Aeson
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 的所有版本?
**注:两个版本的APIreturns是同一种数据。
如果响应的内容类型不同于 "application/json"
.,Network.Wreq.asJSON
将 throw a JSONError
我想最简单的方法是明确使用 Aeson 将响应主体解码为 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
我正在使用 Network.Wreq、Control.Lens、Data.Aeson
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 的所有版本?
**注:两个版本的APIreturns是同一种数据。
"application/json"
.,Network.Wreq.asJSON
将 throw a JSONError
我想最简单的方法是明确使用 Aeson 将响应主体解码为 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