class HTTPX::Response

  1. lib/httpx/response.rb
  2. lib/httpx/response/body.rb
  3. lib/httpx/response/buffer.rb
  4. lib/httpx/transcoder/utils/deflater.rb
  5. show all
Superclass: Object

Defines a HTTP response is handled internally, with a few properties exposed as attributes.

It delegates the following methods to the corresponding HTTPX::Request:

It implements (indirectly, via the body) the IO write protocol to internally buffer payloads.

It implements the IO reader protocol in order for users to buffer/stream it, acts as an enumerable (of payload chunks).

Included modules

  1. Callbacks

Attributes

body [R]

a HTTPX::Response::Body object wrapping the response body. The following methods are delegated to it:

headers [R]

an HTTPX::Headers object containing the response HTTP headers.

status [R]

the HTTP response status code

version [R]

The HTTP protocol version used to fetch the response.

Public Class methods

new(request, status, version, headers)

inits the instance with the corresponding request to this response, an the response HTTP status, version and HTTPX::Headers instance of headers.

[show source]
   # File lib/httpx/response.rb
63 def initialize(request, status, version, headers)
64   @request = request
65   @options = request.options
66   @version = version
67   @status = Integer(status)
68   @headers = @options.headers_class.new(headers)
69   @body = @options.response_body_class.new(self, @options)
70   @finished = complete?
71   @content_type = nil
72 end

Public Instance methods

<<(data)

writes data chunk into the response body.

[show source]
   # File lib/httpx/response.rb
94 def <<(data)
95   @body.write(data)
96 end
bodyless?()

returns whether the response contains body payload.

[show source]
    # File lib/httpx/response.rb
119 def bodyless?
120   @request.verb == "HEAD" ||
121     @status < 200 || # informational response
122     @status == 204 ||
123     @status == 205 ||
124     @status == 304 || begin
125       content_length = @headers["content-length"]
126       return false if content_length.nil?
127 
128       content_length == "0"
129     end
130 end
close()

closes the respective +@request+ and +@body+.

[show source]
   # File lib/httpx/response.rb
83 def close
84   @request.close
85   @body.close
86 end
complete?()
[show source]
    # File lib/httpx/response.rb
132 def complete?
133   bodyless? || (@request.verb == "CONNECT" && @status == 200)
134 end
content_type()

returns the HTTPX::ContentType for the response, as per what’s declared in the content-type header.

response.content_type #=> #<HTTPX::ContentType:xxx @header_value="text/plain">
response.content_type.mime_type #=> "text/plain"
[show source]
    # File lib/httpx/response.rb
102 def content_type
103   @content_type ||= ContentType.new(@headers["content-type"])
104 end
error()

returns an instance of HTTPX::HTTPError if the response has a 4xx or 5xx status code, or nothing.

ok_response.error #=> nil
not_found_response.error #=> HTTPX::HTTPError instance, status 404
[show source]
    # File lib/httpx/response.rb
151 def error
152   return if @status < 400
153 
154   HTTPError.new(self)
155 end
finish!()

marks the response as finished, freezes the headers.

[show source]
    # File lib/httpx/response.rb
112 def finish!
113   @finished = true
114   @headers.freeze
115   @request.connection = nil
116 end
finished?()

returns whether the response has been fully fetched.

[show source]
    # File lib/httpx/response.rb
107 def finished?
108   @finished
109 end
form()

decodes the response payload into a ruby object if the payload is valid “application/x-www-urlencoded” or “multipart/form-data”.

[show source]
    # File lib/httpx/response.rb
177 def form
178   decode(Transcoder::Form)
179 end
initialize_dup(orig)

dupped initialization

[show source]
   # File lib/httpx/response.rb
75 def initialize_dup(orig)
76   super
77   # if a response gets dupped, the body handle must also get dupped to prevent
78   # two responses from using the same file handle to read.
79   @body = orig.body.dup
80 end
inspect()

:nocov:

[show source]
    # File lib/httpx/response.rb
137 def inspect
138   "#<#{self.class}:#{object_id} " \
139     "HTTP/#{version} " \
140     "@status=#{@status} " \
141     "@headers=#{@headers} " \
142     "@body=#{@body.bytesize}>"
143 end
json(*args)

decodes the response payload into a ruby object if the payload is valid json.

response.json #≈> { "foo" => "bar" } for "{\"foo\":\"bar\"}" payload
response.json(symbolize_names: true) #≈> { foo: "bar" } for "{\"foo\":\"bar\"}" payload
[show source]
    # File lib/httpx/response.rb
171 def json(*args)
172   decode(Transcoder::JSON, *args)
173 end
merge_headers(h)

merges headers defined in h into the response headers.

[show source]
   # File lib/httpx/response.rb
89 def merge_headers(h)
90   @headers = @headers.merge(h)
91 end
raise_for_status()

it raises the exception returned by error, or itself otherwise.

ok_response.raise_for_status #=> ok_response
not_found_response.raise_for_status #=> raises HTTPX::HTTPError exception
[show source]
    # File lib/httpx/response.rb
161 def raise_for_status
162   return self unless (err = error)
163 
164   raise err
165 end
xml()
[show source]
    # File lib/httpx/response.rb
181 def xml
182   # TODO: remove at next major version.
183   warn "DEPRECATION WARNING: calling `.#{__method__}` on plain HTTPX responses is deprecated. " \
184        "Use HTTPX.plugin(:xml) sessions and call `.#{__method__}` in its responses instead."
185   require "httpx/plugins/xml"
186   decode(Plugins::XML::Transcoder)
187 end