wojtekmach/req

JSON returned, but not parsed as JSON

Arsenalist opened this issue · 3 comments

I'm calling a few Facebook APIs and even though JSON is returned from the APIs, I'm forced to manually parse it using Jason.decode() instead of Req doing it automatically. Basically, the value is a string of JSON instead of a Map.

result = Req.post!("https://graph.facebook.com...").body

This doesn't happen on all calls but some. Is there a way I can force it to decode as JSON?

what is the content-type of responses that aren’t automatically decoded?

you can add a response step that always decodes:

Req.new(decode_body: false)
|> Req.Request.append_response_steps(
  decode_json: fn req, resp ->
    {req, update_in(resp.body, &Jason.decode!/1}
  end
)

ping

what is the content-type of responses that aren’t automatically decoded?

you can add a response step that always decodes:

Req.new(decode_body: false)
|> Req.Request.append_response_steps(
  decode_json: fn req, resp ->
    {req, update_in(resp.body, &Jason.decode!/1}
  end
)

Sorry about the delay. Thank you for this approach. I'm using Meta's Marketing API and though JSON is returned, it doesn't seem to parse it. The content-header is application/json. However, doing what you suggested works fine. Thank you.