Method: HTTPX::Request#initialize

Defined in:
lib/httpx/request.rb

#initialize(verb, uri, options, params = EMPTY_HASH) ⇒ Request

initializes the instance with the given verb (an upppercase String, ex. ‘GEt’), an absolute or relative uri (either as String or URI::HTTP object), the request options (instance of HTTPX::Options) and an optional Hash of params.

Besides any of the options documented in HTTPX::Options (which would override or merge with what options sets), it accepts also the following:

:params

hash or array of key-values which will be encoded and set in the query string of request uris.

:body

to be encoded in the request body payload. can be a String, an IO object (i.e. a File), or an Enumerable.

:form

hash of array of key-values which will be form-urlencoded- or multipart-encoded in requests body payload.

:json

hash of array of key-values which will be JSON-encoded in requests body payload.

:xml

Nokogiri XML nodes which will be encoded in requests body payload.

:body, :form, :json and :xml are all mutually exclusive, i.e. only one of them gets picked up.



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
# File 'lib/httpx/request.rb', line 69

def initialize(verb, uri, options, params = EMPTY_HASH)
  @verb    = verb.to_s.upcase
  @uri     = Utils.to_uri(uri)

  @headers = options.headers.dup
  merge_headers(params.delete(:headers)) if params.key?(:headers)

  @query_params = params.delete(:params) if params.key?(:params)

  @body = options.request_body_class.new(@headers, options, **params)

  @options = @body.options

  if @uri.relative? || @uri.host.nil?
    origin = @options.origin
    raise(Error, "invalid URI: #{@uri}") unless origin

    base_path = @options.base_path

    @uri = origin.merge("#{base_path}#{@uri}")
  end

  raise UnsupportedSchemeError, "#{@uri}: #{@uri.scheme}: unsupported URI scheme" unless ALLOWED_URI_SCHEMES.include?(@uri.scheme)

  @state = :idle
  @response = @drainer = @peer_address = @informational_status = nil
  @ping = false
  @persistent = @options.persistent
  @active_timeouts = []
end