layout | title | permalink | hide | prev_name | prev_link | next_name | next_link | top_name | top_link |
---|---|---|---|---|---|---|---|---|---|
documentation |
JSON Response Middleware |
/middleware/json-response |
true |
Instrumentation Middleware |
./instrumentation |
Logger Middleware |
./logger |
Back to Middleware |
./list |
The Json Response
middleware parses response body into a hash of key/value pairs.
The behaviour can be customized with the following options:
- parser_options: options that will be sent to the JSON.parse method. Defaults to {}
- content_type: Single value or Array of response content-types that should be processed. Can be either strings or Regex. Defaults to
/\bjson$/
- preserve_raw: If set to true, the original un-parsed response will be stored in the
response.env[:raw_body]
property. Defaults tofalse
conn = Faraday.new('http://httpbingo.org') do |f|
f.response :json, **options
end
conn.get('json').body
# => {"slideshow"=>{"author"=>"Yours Truly", "date"=>"date of publication", "slides"=>[{"title"=>"Wake up to WonderWidgets!", "type"=>"all"}, {"items"=>["Why <em>WonderWidgets</em> are great", "Who <em>buys</em> WonderWidgets"], "title"=>"Overview", "type"=>"all"}], "title"=>"Sample Slide Show"}}