lostisland / faraday_middleware

Various Faraday middlewares for Faraday-based API wrappers
MIT License
557 stars 203 forks source link

Gzip middleware seems mandatory when used with json response middleware? #262

Closed venki09 closed 3 years ago

venki09 commented 4 years ago

According to the comment mentioned in here https://github.com/lostisland/faraday_middleware/blob/master/lib/faraday_middleware/gzip.rb#L11-L14 it seems like we don't need to install this middleware when net_http adapter is used. But when I use the json response middleware, I am getting an error when the response middleware tries to parse the response body because it is trying to parse a compressed body.

I am able to get it to work by following instructions mentioned here https://github.com/lostisland/faraday_middleware/issues/153#issuecomment-290768653 (Install Gzip middleware after the json response middleware) but then it looks like I needed to install gzip middleware even though I am using net_http adapter. Let me know if I am missing something trivial or if my question is not clear.

iMacTia commented 4 years ago

Hi @venki09, the Gzip middleware is definitely not necessary with the JSON. Your issue might be due to several things, like the middleware order or the server response. Could you please share the following:

hoffmanilya commented 3 years ago

I'm seeing the same behavior. Running the script below ends with a Faraday::ParsingError and uncommenting the gzip middleware seems to fix the issue.

require 'faraday'
require 'faraday_middleware'

connection = Faraday.new do |builder|
  builder.response :json
  # builder.use :gzip
end

res = connection.get('https://jsonplaceholder.typicode.com/posts/1') do |req|
  req.headers['Accept-Encoding'] = 'gzip'
end

puts res.body

faraday (1.5.1) faraday_middleware (1.0.0)

iMacTia commented 3 years ago

@hoffmanilya now I see! That's because you're explicitly asking the server for a gzip response, by setting the Accept-Encoding header. That means the response you get is gzipped, and obviously you need to un-gzip it before you can parse it 😄.

The below will work just fine:

require 'faraday'
require 'faraday_middleware'

connection = Faraday.new do |builder|
  builder.response :json
  # builder.use :gzip
end

res = connection.get('https://jsonplaceholder.typicode.com/posts/1')

puts res.body

Including the JSON parsing of the response. So as you can see the gzip middleware is not mandatory when you use the json one, but it does become mandatory if you ask the server for a gzipped response (or if the server returns a gzipped response by default).

Closing this now, but feel free to come back if you need more help!

hoffmanilya commented 3 years ago

Thanks a lot for the quick reply! The way I read the comment made it seem like net_http would automatically handle the decompression but what you're saying makes sense.