Class: Faraday::Request

Inherits:
Struct
  • Object
show all
Extended by:
AutoloadHelper, MiddlewareRegistry
Defined in:
lib/faraday/request.rb,
lib/faraday/autoload.rb,
lib/faraday/request/retry.rb,
lib/faraday/request/multipart.rb,
lib/faraday/request/url_encoded.rb,
lib/faraday/request/authorization.rb,
lib/faraday/request/instrumentation.rb,
lib/faraday/request/basic_authentication.rb,
lib/faraday/request/token_authentication.rb

Overview

Request represents a single HTTP request for a Faraday adapter to make.

See Also:

  • Original class location

Defined Under Namespace

Classes: Authorization, BasicAuthentication, Instrumentation, Multipart, Retry, TokenAuthentication, UrlEncoded

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MiddlewareRegistry

fetch_middleware, load_middleware, lookup_middleware, middleware_mutex, register_middleware, unregister_middleware

Methods included from AutoloadHelper

all_loaded_constants, autoload_all, load_autoloaded_constants

Instance Attribute Details

#bodyHash

Returns body.

Returns:

  • (Hash)

    body



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/faraday/request.rb', line 29

class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
  # rubocop:enable Style/StructInheritance

  extend MiddlewareRegistry

  register_middleware File.expand_path('request', __dir__),
                      url_encoded: [:UrlEncoded, 'url_encoded'],
                      multipart: [:Multipart, 'multipart'],
                      retry: [:Retry, 'retry'],
                      authorization: [:Authorization, 'authorization'],
                      basic_auth: [
                        :BasicAuthentication,
                        'basic_authentication'
                      ],
                      token_auth: [
                        :TokenAuthentication,
                        'token_authentication'
                      ],
                      instrumentation: [:Instrumentation, 'instrumentation']

  # @param request_method [String]
  # @yield [request] for block customization, if block given
  # @yieldparam request [Request]
  # @return [Request]
  def self.create(request_method)
    new(request_method).tap do |request|
      yield(request) if block_given?
    end
  end

  # Replace params, preserving the existing hash type.
  #
  # @param hash [Hash] new params
  def params=(hash)
    if params
      params.replace hash
    else
      super
    end
  end

  # Replace request headers, preserving the existing hash type.
  #
  # @param hash [Hash] new headers
  def headers=(hash)
    if headers
      headers.replace hash
    else
      super
    end
  end

  # Update path and params.
  #
  # @param path [URI, String]
  # @param params [Hash, nil]
  # @return [void]
  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

  # @param key [Object] key to look up in headers
  # @return [Object] value of the given header name
  def [](key)
    headers[key]
  end

  # @param key [Object] key of header to write
  # @param value [Object] value of header
  def []=(key, value)
    headers[key] = value
  end

  # Marshal serialization support.
  #
  # @return [Hash] the hash ready to be serialized in Marshal.
  def marshal_dump
    {
      method: method,
      body: body,
      headers: headers,
      path: path,
      params: params,
      options: options
    }
  end

  # Marshal serialization support.
  # Restores the instance variables according to the +serialised+.
  # @param serialised [Hash] the serialised object.
  def marshal_load(serialised)
    self.method  = serialised[:method]
    self.body    = serialised[:body]
    self.headers = serialised[:headers]
    self.path    = serialised[:path]
    self.params  = serialised[:params]
    self.options = serialised[:options]
  end

  # @return [Env] the Env for this Request
  def to_env(connection)
    Env.new(method, body, connection.build_exclusive_url(path, params),
            options, headers, connection.ssl, connection.parallel_manager)
  end
end

#headersFaraday::Utils::Headers

Returns headers.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/faraday/request.rb', line 29

class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
  # rubocop:enable Style/StructInheritance

  extend MiddlewareRegistry

  register_middleware File.expand_path('request', __dir__),
                      url_encoded: [:UrlEncoded, 'url_encoded'],
                      multipart: [:Multipart, 'multipart'],
                      retry: [:Retry, 'retry'],
                      authorization: [:Authorization, 'authorization'],
                      basic_auth: [
                        :BasicAuthentication,
                        'basic_authentication'
                      ],
                      token_auth: [
                        :TokenAuthentication,
                        'token_authentication'
                      ],
                      instrumentation: [:Instrumentation, 'instrumentation']

  # @param request_method [String]
  # @yield [request] for block customization, if block given
  # @yieldparam request [Request]
  # @return [Request]
  def self.create(request_method)
    new(request_method).tap do |request|
      yield(request) if block_given?
    end
  end

  # Replace params, preserving the existing hash type.
  #
  # @param hash [Hash] new params
  def params=(hash)
    if params
      params.replace hash
    else
      super
    end
  end

  # Replace request headers, preserving the existing hash type.
  #
  # @param hash [Hash] new headers
  def headers=(hash)
    if headers
      headers.replace hash
    else
      super
    end
  end

  # Update path and params.
  #
  # @param path [URI, String]
  # @param params [Hash, nil]
  # @return [void]
  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

  # @param key [Object] key to look up in headers
  # @return [Object] value of the given header name
  def [](key)
    headers[key]
  end

  # @param key [Object] key of header to write
  # @param value [Object] value of header
  def []=(key, value)
    headers[key] = value
  end

  # Marshal serialization support.
  #
  # @return [Hash] the hash ready to be serialized in Marshal.
  def marshal_dump
    {
      method: method,
      body: body,
      headers: headers,
      path: path,
      params: params,
      options: options
    }
  end

  # Marshal serialization support.
  # Restores the instance variables according to the +serialised+.
  # @param serialised [Hash] the serialised object.
  def marshal_load(serialised)
    self.method  = serialised[:method]
    self.body    = serialised[:body]
    self.headers = serialised[:headers]
    self.path    = serialised[:path]
    self.params  = serialised[:params]
    self.options = serialised[:options]
  end

  # @return [Env] the Env for this Request
  def to_env(connection)
    Env.new(method, body, connection.build_exclusive_url(path, params),
            options, headers, connection.ssl, connection.parallel_manager)
  end
end

#methodSymbol

Returns the HTTP method of the Request.

Returns:

  • (Symbol)

    the HTTP method of the Request



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/faraday/request.rb', line 29

class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
  # rubocop:enable Style/StructInheritance

  extend MiddlewareRegistry

  register_middleware File.expand_path('request', __dir__),
                      url_encoded: [:UrlEncoded, 'url_encoded'],
                      multipart: [:Multipart, 'multipart'],
                      retry: [:Retry, 'retry'],
                      authorization: [:Authorization, 'authorization'],
                      basic_auth: [
                        :BasicAuthentication,
                        'basic_authentication'
                      ],
                      token_auth: [
                        :TokenAuthentication,
                        'token_authentication'
                      ],
                      instrumentation: [:Instrumentation, 'instrumentation']

  # @param request_method [String]
  # @yield [request] for block customization, if block given
  # @yieldparam request [Request]
  # @return [Request]
  def self.create(request_method)
    new(request_method).tap do |request|
      yield(request) if block_given?
    end
  end

  # Replace params, preserving the existing hash type.
  #
  # @param hash [Hash] new params
  def params=(hash)
    if params
      params.replace hash
    else
      super
    end
  end

  # Replace request headers, preserving the existing hash type.
  #
  # @param hash [Hash] new headers
  def headers=(hash)
    if headers
      headers.replace hash
    else
      super
    end
  end

  # Update path and params.
  #
  # @param path [URI, String]
  # @param params [Hash, nil]
  # @return [void]
  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

  # @param key [Object] key to look up in headers
  # @return [Object] value of the given header name
  def [](key)
    headers[key]
  end

  # @param key [Object] key of header to write
  # @param value [Object] value of header
  def []=(key, value)
    headers[key] = value
  end

  # Marshal serialization support.
  #
  # @return [Hash] the hash ready to be serialized in Marshal.
  def marshal_dump
    {
      method: method,
      body: body,
      headers: headers,
      path: path,
      params: params,
      options: options
    }
  end

  # Marshal serialization support.
  # Restores the instance variables according to the +serialised+.
  # @param serialised [Hash] the serialised object.
  def marshal_load(serialised)
    self.method  = serialised[:method]
    self.body    = serialised[:body]
    self.headers = serialised[:headers]
    self.path    = serialised[:path]
    self.params  = serialised[:params]
    self.options = serialised[:options]
  end

  # @return [Env] the Env for this Request
  def to_env(connection)
    Env.new(method, body, connection.build_exclusive_url(path, params),
            options, headers, connection.ssl, connection.parallel_manager)
  end
end

#optionsRequestOptions

Returns options.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/faraday/request.rb', line 29

class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
  # rubocop:enable Style/StructInheritance

  extend MiddlewareRegistry

  register_middleware File.expand_path('request', __dir__),
                      url_encoded: [:UrlEncoded, 'url_encoded'],
                      multipart: [:Multipart, 'multipart'],
                      retry: [:Retry, 'retry'],
                      authorization: [:Authorization, 'authorization'],
                      basic_auth: [
                        :BasicAuthentication,
                        'basic_authentication'
                      ],
                      token_auth: [
                        :TokenAuthentication,
                        'token_authentication'
                      ],
                      instrumentation: [:Instrumentation, 'instrumentation']

  # @param request_method [String]
  # @yield [request] for block customization, if block given
  # @yieldparam request [Request]
  # @return [Request]
  def self.create(request_method)
    new(request_method).tap do |request|
      yield(request) if block_given?
    end
  end

  # Replace params, preserving the existing hash type.
  #
  # @param hash [Hash] new params
  def params=(hash)
    if params
      params.replace hash
    else
      super
    end
  end

  # Replace request headers, preserving the existing hash type.
  #
  # @param hash [Hash] new headers
  def headers=(hash)
    if headers
      headers.replace hash
    else
      super
    end
  end

  # Update path and params.
  #
  # @param path [URI, String]
  # @param params [Hash, nil]
  # @return [void]
  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

  # @param key [Object] key to look up in headers
  # @return [Object] value of the given header name
  def [](key)
    headers[key]
  end

  # @param key [Object] key of header to write
  # @param value [Object] value of header
  def []=(key, value)
    headers[key] = value
  end

  # Marshal serialization support.
  #
  # @return [Hash] the hash ready to be serialized in Marshal.
  def marshal_dump
    {
      method: method,
      body: body,
      headers: headers,
      path: path,
      params: params,
      options: options
    }
  end

  # Marshal serialization support.
  # Restores the instance variables according to the +serialised+.
  # @param serialised [Hash] the serialised object.
  def marshal_load(serialised)
    self.method  = serialised[:method]
    self.body    = serialised[:body]
    self.headers = serialised[:headers]
    self.path    = serialised[:path]
    self.params  = serialised[:params]
    self.options = serialised[:options]
  end

  # @return [Env] the Env for this Request
  def to_env(connection)
    Env.new(method, body, connection.build_exclusive_url(path, params),
            options, headers, connection.ssl, connection.parallel_manager)
  end
end

#paramsHash

Returns query parameters.

Returns:

  • (Hash)

    query parameters



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/faraday/request.rb', line 29

class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
  # rubocop:enable Style/StructInheritance

  extend MiddlewareRegistry

  register_middleware File.expand_path('request', __dir__),
                      url_encoded: [:UrlEncoded, 'url_encoded'],
                      multipart: [:Multipart, 'multipart'],
                      retry: [:Retry, 'retry'],
                      authorization: [:Authorization, 'authorization'],
                      basic_auth: [
                        :BasicAuthentication,
                        'basic_authentication'
                      ],
                      token_auth: [
                        :TokenAuthentication,
                        'token_authentication'
                      ],
                      instrumentation: [:Instrumentation, 'instrumentation']

  # @param request_method [String]
  # @yield [request] for block customization, if block given
  # @yieldparam request [Request]
  # @return [Request]
  def self.create(request_method)
    new(request_method).tap do |request|
      yield(request) if block_given?
    end
  end

  # Replace params, preserving the existing hash type.
  #
  # @param hash [Hash] new params
  def params=(hash)
    if params
      params.replace hash
    else
      super
    end
  end

  # Replace request headers, preserving the existing hash type.
  #
  # @param hash [Hash] new headers
  def headers=(hash)
    if headers
      headers.replace hash
    else
      super
    end
  end

  # Update path and params.
  #
  # @param path [URI, String]
  # @param params [Hash, nil]
  # @return [void]
  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

  # @param key [Object] key to look up in headers
  # @return [Object] value of the given header name
  def [](key)
    headers[key]
  end

  # @param key [Object] key of header to write
  # @param value [Object] value of header
  def []=(key, value)
    headers[key] = value
  end

  # Marshal serialization support.
  #
  # @return [Hash] the hash ready to be serialized in Marshal.
  def marshal_dump
    {
      method: method,
      body: body,
      headers: headers,
      path: path,
      params: params,
      options: options
    }
  end

  # Marshal serialization support.
  # Restores the instance variables according to the +serialised+.
  # @param serialised [Hash] the serialised object.
  def marshal_load(serialised)
    self.method  = serialised[:method]
    self.body    = serialised[:body]
    self.headers = serialised[:headers]
    self.path    = serialised[:path]
    self.params  = serialised[:params]
    self.options = serialised[:options]
  end

  # @return [Env] the Env for this Request
  def to_env(connection)
    Env.new(method, body, connection.build_exclusive_url(path, params),
            options, headers, connection.ssl, connection.parallel_manager)
  end
end

#pathURI, String

Returns the path.

Returns:

  • (URI, String)

    the path



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/faraday/request.rb', line 29

class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
  # rubocop:enable Style/StructInheritance

  extend MiddlewareRegistry

  register_middleware File.expand_path('request', __dir__),
                      url_encoded: [:UrlEncoded, 'url_encoded'],
                      multipart: [:Multipart, 'multipart'],
                      retry: [:Retry, 'retry'],
                      authorization: [:Authorization, 'authorization'],
                      basic_auth: [
                        :BasicAuthentication,
                        'basic_authentication'
                      ],
                      token_auth: [
                        :TokenAuthentication,
                        'token_authentication'
                      ],
                      instrumentation: [:Instrumentation, 'instrumentation']

  # @param request_method [String]
  # @yield [request] for block customization, if block given
  # @yieldparam request [Request]
  # @return [Request]
  def self.create(request_method)
    new(request_method).tap do |request|
      yield(request) if block_given?
    end
  end

  # Replace params, preserving the existing hash type.
  #
  # @param hash [Hash] new params
  def params=(hash)
    if params
      params.replace hash
    else
      super
    end
  end

  # Replace request headers, preserving the existing hash type.
  #
  # @param hash [Hash] new headers
  def headers=(hash)
    if headers
      headers.replace hash
    else
      super
    end
  end

  # Update path and params.
  #
  # @param path [URI, String]
  # @param params [Hash, nil]
  # @return [void]
  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

  # @param key [Object] key to look up in headers
  # @return [Object] value of the given header name
  def [](key)
    headers[key]
  end

  # @param key [Object] key of header to write
  # @param value [Object] value of header
  def []=(key, value)
    headers[key] = value
  end

  # Marshal serialization support.
  #
  # @return [Hash] the hash ready to be serialized in Marshal.
  def marshal_dump
    {
      method: method,
      body: body,
      headers: headers,
      path: path,
      params: params,
      options: options
    }
  end

  # Marshal serialization support.
  # Restores the instance variables according to the +serialised+.
  # @param serialised [Hash] the serialised object.
  def marshal_load(serialised)
    self.method  = serialised[:method]
    self.body    = serialised[:body]
    self.headers = serialised[:headers]
    self.path    = serialised[:path]
    self.params  = serialised[:params]
    self.options = serialised[:options]
  end

  # @return [Env] the Env for this Request
  def to_env(connection)
    Env.new(method, body, connection.build_exclusive_url(path, params),
            options, headers, connection.ssl, connection.parallel_manager)
  end
end

Class Method Details

.create(request_method) {|request| ... } ⇒ Request

Parameters:

  • request_method (String)

Yields:

  • (request)

    for block customization, if block given

Yield Parameters:

Returns:



53
54
55
56
57
# File 'lib/faraday/request.rb', line 53

def self.create(request_method)
  new(request_method).tap do |request|
    yield(request) if block_given?
  end
end

Instance Method Details

#[](key) ⇒ Object

Returns value of the given header name.

Parameters:

  • key (Object)

    key to look up in headers

Returns:

  • (Object)

    value of the given header name



104
105
106
# File 'lib/faraday/request.rb', line 104

def [](key)
  headers[key]
end

#[]=(key, value) ⇒ Object

Parameters:

  • key (Object)

    key of header to write

  • value (Object)

    value of header



110
111
112
# File 'lib/faraday/request.rb', line 110

def []=(key, value)
  headers[key] = value
end

#marshal_dumpHash

Marshal serialization support.

Returns:

  • (Hash)

    the hash ready to be serialized in Marshal.



117
118
119
120
121
122
123
124
125
126
# File 'lib/faraday/request.rb', line 117

def marshal_dump
  {
    method: method,
    body: body,
    headers: headers,
    path: path,
    params: params,
    options: options
  }
end

#marshal_load(serialised) ⇒ Object

Marshal serialization support. Restores the instance variables according to the serialised.

Parameters:

  • serialised (Hash)

    the serialised object.



131
132
133
134
135
136
137
138
# File 'lib/faraday/request.rb', line 131

def marshal_load(serialised)
  self.method  = serialised[:method]
  self.body    = serialised[:body]
  self.headers = serialised[:headers]
  self.path    = serialised[:path]
  self.params  = serialised[:params]
  self.options = serialised[:options]
end

#to_env(connection) ⇒ Env

Returns the Env for this Request.

Returns:

  • (Env)

    the Env for this Request



141
142
143
144
# File 'lib/faraday/request.rb', line 141

def to_env(connection)
  Env.new(method, body, connection.build_exclusive_url(path, params),
          options, headers, connection.ssl, connection.parallel_manager)
end

#url(path, params = nil) ⇒ void

This method returns an undefined value.

Update path and params.

Parameters:

  • path (URI, String)
  • params (Hash, nil) (defaults to: nil)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/faraday/request.rb', line 86

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