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 86 def <<(data) 87 @body.write(data) 88 end
returns whether the response contains body payload.
# File lib/httpx/response.rb 110 def bodyless? 111 @request.verb == "HEAD" || 112 @status < 200 || # informational response 113 @status == 204 || 114 @status == 205 || 115 @status == 304 || begin 116 content_length = @headers["content-length"] 117 return false if content_length.nil? 118 119 content_length == "0" 120 end 121 end
closes the respective +@request+ and +@body+.
# File lib/httpx/response.rb 75 def close 76 @request.close 77 @body.close 78 end
# File lib/httpx/response.rb 123 def complete? 124 bodyless? || (@request.verb == "CONNECT" && @status == 200) 125 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 94 def content_type 95 @content_type ||= ContentType.new(@headers["content-type"]) 96 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 142 def error 143 return if @status < 400 144 145 HTTPError.new(self) 146 end
marks the response as finished, freezes the headers.
# File lib/httpx/response.rb 104 def finish! 105 @finished = true 106 @headers.freeze 107 end
returns whether the response has been fully fetched.
# File lib/httpx/response.rb 99 def finished? 100 @finished 101 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 168 def form 169 decode(Transcoder::Form) 170 end
:nocov:
# File lib/httpx/response.rb 128 def inspect 129 "#<Response:#{object_id} " \ 130 "HTTP/#{version} " \ 131 "@status=#{@status} " \ 132 "@headers=#{@headers} " \ 133 "@body=#{@body.bytesize}>" 134 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 162 def json(*args) 163 decode(Transcoder::JSON, *args) 164 end
merges headers defined in h
into the response headers.
# File lib/httpx/response.rb 81 def merge_headers(h) 82 @headers = @headers.merge(h) 83 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 152 def raise_for_status 153 return self unless (err = error) 154 155 raise err 156 end
# File lib/httpx/response.rb 172 def xml 173 # TODO: remove at next major version. 174 warn "DEPRECATION WARNING: calling `.#{__method__}` on plain HTTPX responses is deprecated. " \ 175 "Use HTTPX.plugin(:xml) sessions and call `.#{__method__}` in its responses instead." 176 require "httpx/plugins/xml" 177 decode(Plugins::XML::Transcoder) 178 end