class Faraday::Request

Request represents a single HTTP request for a Faraday adapter to make. @see lib/faraday/request.rb Original class location

Used to setup URLs, params, headers, and the request body in a sane manner.

@example

@connection.post do |req|
  req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
  req.headers['b'] = '2' # Header
  req.params['c']  = '3' # GET Param
  req['b']         = '2' # also Header
  req.body = 'abc'
end

@!attribute http_method

@return [Symbol] the HTTP method of the Request

@!attribute path

@return [URI, String] the path

@!attribute params

@return [Hash] query parameters

@!attribute headers

@return [Faraday::Utils::Headers] headers

@!attribute body

@return [Hash] body

@!attribute options

@return [RequestOptions] options

rubocop:disable Style/StructInheritance

Public Class Methods

create(request_method) { |request| ... } click to toggle source

@param request_method [String] @yield [request] for block customization, if block given @yieldparam request [Request] @return [Request]

# File lib/faraday/request.rb, line 55
def self.create(request_method)
  new(request_method).tap do |request|
    yield(request) if block_given?
  end
end

Public Instance Methods

[](key) click to toggle source

@param key [Object] key to look up in headers @return [Object] value of the given header name

# File lib/faraday/request.rb, line 114
def [](key)
  headers[key]
end
[]=(key, value) click to toggle source

@param key [Object] key of header to write @param value [Object] value of header

# File lib/faraday/request.rb, line 120
def []=(key, value)
  headers[key] = value
end
headers=(hash) click to toggle source

Replace request headers, preserving the existing hash type.

@param hash [Hash] new headers

Calls superclass method
# File lib/faraday/request.rb, line 83
def headers=(hash)
  if headers
    headers.replace hash
  else
    super
  end
end
marshal_dump() click to toggle source

Marshal serialization support.

@return [Hash] the hash ready to be serialized in Marshal.

# File lib/faraday/request.rb, line 127
def marshal_dump
  {
    http_method: http_method,
    body: body,
    headers: headers,
    path: path,
    params: params,
    options: options
  }
end
marshal_load(serialised) click to toggle source

Marshal serialization support. Restores the instance variables according to the serialised. @param serialised [Hash] the serialised object.

# File lib/faraday/request.rb, line 141
def marshal_load(serialised)
  self.http_method = serialised[:http_method]
  self.body        = serialised[:body]
  self.headers     = serialised[:headers]
  self.path        = serialised[:path]
  self.params      = serialised[:params]
  self.options     = serialised[:options]
end
method() click to toggle source
# File lib/faraday/request.rb, line 61
    def method
      warn <<~TEXT
        WARNING: `Faraday::Request##{__method__}` is deprecated; use `#http_method` instead. It will be removed in or after version 2.0.
        `Faraday::Request##{__method__}` called from #{caller_locations(1..1).first}
      TEXT
      http_method
    end
params=(hash) click to toggle source

Replace params, preserving the existing hash type.

@param hash [Hash] new params

Calls superclass method
# File lib/faraday/request.rb, line 72
def params=(hash)
  if params
    params.replace hash
  else
    super
  end
end
to_env(connection) click to toggle source

@return [Env] the Env for this Request

# File lib/faraday/request.rb, line 151
def to_env(connection)
  Env.new(http_method, body, connection.build_exclusive_url(path, params),
          options, headers, connection.ssl, connection.parallel_manager)
end
url(path, params = nil) click to toggle source

Update path and params.

@param path [URI, String] @param params [Hash, nil] @return [void]

# File lib/faraday/request.rb, line 96
def url(path, params = nil)
  if path.respond_to? :query
    if (query = path.query)
      path = path.dup
      path.query = nil
    end
  else
    anchor_index = path.index('#')
    path = path.slice(0, anchor_index) unless anchor_index.nil?
    path, query = path.split('?', 2)
  end
  self.path = path
  self.params.merge_query query, options.params_encoder
  self.params.update(params) if params
end