Closed venki09 closed 3 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:
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)
@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!
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.
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.