Class: Faraday::Request::Json

Inherits:
Middleware show all
Defined in:
lib/faraday/request/json.rb

Overview

Request middleware that encodes the body as JSON.

Processes only requests with matching Content-type or those without a type. If a request doesn’t have a type but has a body, it sets the Content-type to JSON MIME-type.

Doesn’t try to encode bodies that already are in string form.

Constant Summary collapse

MIME_TYPE =
'application/json'
MIME_TYPE_REGEX =
%r{^application/(vnd\..+\+)?json$}.freeze

Instance Attribute Summary

Attributes inherited from Middleware

#app, #options

Instance Method Summary collapse

Methods inherited from Middleware

#call, #close, #initialize

Methods included from MiddlewareRegistry

#lookup_middleware, #register_middleware, #registered_middleware, #unregister_middleware

Constructor Details

This class inherits a constructor from Faraday::Middleware

Instance Method Details

#on_request(env) ⇒ Object



18
19
20
21
22
# File 'lib/faraday/request/json.rb', line 18

def on_request(env)
  match_content_type(env) do |data|
    env[:body] = encode(data)
  end
end