Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faraday doesn't return JSON response when upgrade to v2.10.11 #1591

Open
minhtienvu opened this issue Sep 13, 2024 · 2 comments
Open

Faraday doesn't return JSON response when upgrade to v2.10.11 #1591

minhtienvu opened this issue Sep 13, 2024 · 2 comments

Comments

@minhtienvu
Copy link

Basic Info

  • Faraday Version: 2.10.1
  • Ruby Version: 3.1.4

Issue description

  • I'm upgrading my Rails app from version 6.1.7.6 to 7.0.8.4. This will force Faraday to version v2.

Steps to reproduce

This is my current code

client ||= Faraday.new(Settings.url) do |faraday|
  faraday.headers["X-Access-Key"] = access_key if access_key
  faraday.request :json
  faraday.response :json
end

client.post(
  Settings.url,
  {
    "name" => "John Doe",
  }.to_json
)

My Rails app used Faraday v0.9 very well and returned a response like this

{"reason"=>"invalid"}

But right now, with Faraday v2.10.1, it returns

"{\"reason\":\"invalid\"}"

Some ways I tried but didn't work:

  1. Change content-type
client ||= Faraday.new(Settings.url) do |faraday|
  faraday.headers["X-Access-Key"] = access_key if access_key
  faraday.request :json
  faraday.response :json, content_type: /\bjson$/
end
  1. Add a adapter
client ||= Faraday.new(Settings.url) do |faraday|
  faraday.headers["X-Access-Key"] = access_key if access_key
  faraday.request :json
  faraday.response :json
  faraday.adapter :net_http
end

CHECKLIST (delete before creating the issue)

  • If you're not reporting a bug/issue, you can ignore this whole template.
  • Are you using the latest Faraday version? If not, please check the Releases page to see if the issue has already been fixed.
  • Provide the Faraday and Ruby Version you're using while experiencing the issue.
  • Fill the Issue description and Steps to Reproduce sections.
  • Delete this checklist before posting the issue.
@olleolleolle
Copy link
Member

👋 Oh!

https://lostisland.github.io/faraday/#/middleware/included/json

The to_json should now be optional, since the request middleware part of the json middleware would make that attempt for you.

Is the server's response using a Content-Type that matches what json middleware wants to deal with?

There was a cool note here, that you can add a preserve_raw: true option, to see the unprocessed values Faraday got, as well, if you suspect something goes wrong in decoding:
https://lostisland.github.io/faraday/#/middleware/included/json?id=json-responses

@iMacTia
Copy link
Member

iMacTia commented Sep 13, 2024

Adding to @olleolleolle comment, I'd also suggest to add the logger middleware and share the response logs with us, so we can see if there's anything weird with the response.

Please note by default the logger only logs the response headers, so make sure you pass the right options to it (and obviously redact any sensitive data before sharing them with us):

  faraday.response :logger, nil, { headers: true, bodies: true, errors: true }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants