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).
Methods
Public Class
Public Instance
Included modules
Classes and Modules
Attributes
body | [R] |
a |
headers | [R] |
an |
status | [R] |
the HTTP response status code |
version | [R] |
The HTTP protocol version used to fetch the response. |
Public Class methods
inits the instance with the corresponding request
to this response, an the response HTTP status
, version
and HTTPX::Headers
instance of headers
.
# 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
writes data
chunk into the response body.
# File lib/httpx/response.rb 94 def <<(data) 95 @body.write(data) 96 end
returns whether the response contains body payload.
# File lib/httpx/response.rb 118 def bodyless? 119 @request.verb == "HEAD" || 120 @status < 200 || # informational response 121 @status == 204 || 122 @status == 205 || 123 @status == 304 || begin 124 content_length = @headers["content-length"] 125 return false if content_length.nil? 126 127 content_length == "0" 128 end 129 end
closes the respective +@request+ and +@body+.
# File lib/httpx/response.rb 83 def close 84 @request.close 85 @body.close 86 end
# File lib/httpx/response.rb 131 def complete? 132 bodyless? || (@request.verb == "CONNECT" && @status == 200) 133 end
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"
# File lib/httpx/response.rb 102 def content_type 103 @content_type ||= ContentType.new(@headers["content-type"]) 104 end
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
# File lib/httpx/response.rb 150 def error 151 return if @status < 400 152 153 HTTPError.new(self) 154 end
marks the response as finished, freezes the headers.
# File lib/httpx/response.rb 112 def finish! 113 @finished = true 114 @headers.freeze 115 end
returns whether the response has been fully fetched.
# File lib/httpx/response.rb 107 def finished? 108 @finished 109 end
decodes the response payload into a ruby object if the payload is valid “application/x-www-urlencoded” or “multipart/form-data”.
# File lib/httpx/response.rb 176 def form 177 decode(Transcoder::Form) 178 end
dupped initialization
# 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
:nocov:
# File lib/httpx/response.rb 136 def inspect 137 "#<#{self.class}:#{object_id} " \ 138 "HTTP/#{version} " \ 139 "@status=#{@status} " \ 140 "@headers=#{@headers} " \ 141 "@body=#{@body.bytesize}>" 142 end
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
# File lib/httpx/response.rb 170 def json(*args) 171 decode(Transcoder::JSON, *args) 172 end
merges headers defined in h
into the response headers.
# File lib/httpx/response.rb 89 def merge_headers(h) 90 @headers = @headers.merge(h) 91 end
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
# File lib/httpx/response.rb 160 def raise_for_status 161 return self unless (err = error) 162 163 raise err 164 end
# File lib/httpx/response.rb 180 def xml 181 # TODO: remove at next major version. 182 warn "DEPRECATION WARNING: calling `.#{__method__}` on plain HTTPX responses is deprecated. " \ 183 "Use HTTPX.plugin(:xml) sessions and call `.#{__method__}` in its responses instead." 184 require "httpx/plugins/xml" 185 decode(Plugins::XML::Transcoder) 186 end