class Faraday::Request::UrlEncoded

Middleware for supporting urlencoded requests.

Constants

CONTENT_TYPE

Attributes

Public Instance Methods

Encodes as “application/x-www-form-urlencoded” if not already encoded or of another type.

@param env [Faraday::Env]

# File lib/faraday/request/url_encoded.rb, line 20
def call(env)
  match_content_type(env) do |data|
    params = Faraday::Utils::ParamsHash[data]
    env.body = params.to_query(env.params_encoder)
  end
  @app.call env
end

@param env [Faraday::Env] @yield [request_body] Body of the request

# File lib/faraday/request/url_encoded.rb, line 30
def match_content_type(env)
  return unless process_request?(env)

  env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
  return if env.body.respond_to?(:to_str) || env.body.respond_to?(:read)

  yield(env.body)
end

@param env [Faraday::Env]

@return [Boolean] True if the request has a body and its Content-Type is

urlencoded.
# File lib/faraday/request/url_encoded.rb, line 43
def process_request?(env)
  type = request_type(env)
  env.body && (type.empty? || (type == self.class.mime_type))
end

@param env [Faraday::Env]

@return [String]

# File lib/faraday/request/url_encoded.rb, line 51
def request_type(env)
  type = env.request_headers[CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end