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 66 def initialize(request, status, version, headers) 67 @request = request 68 @options = request.options 69 @version = version 70 @status = Integer(status) 71 @headers = @options.headers_class.new(headers) 72 @body = @options.response_body_class.new(self, @options) 73 @finished = complete? 74 @content_type = nil 75 end
Public Instance methods
writes data
chunk into the response body.
# File lib/httpx/response.rb 83 def <<(data) 84 @body.write(data) 85 end
returns whether the response contains body payload.
# File lib/httpx/response.rb 107 def bodyless? 108 @request.verb == "HEAD" || 109 @status < 200 || # informational response 110 @status == 204 || 111 @status == 205 || 112 @status == 304 || begin 113 content_length = @headers["content-length"] 114 return false if content_length.nil? 115 116 content_length == "0" 117 end 118 end
# File lib/httpx/response.rb 120 def complete? 121 bodyless? || (@request.verb == "CONNECT" && @status == 200) 122 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 91 def content_type 92 @content_type ||= ContentType.new(@headers["content-type"]) 93 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 139 def error 140 return if @status < 400 141 142 HTTPError.new(self) 143 end
marks the response as finished, freezes the headers.
# File lib/httpx/response.rb 101 def finish! 102 @finished = true 103 @headers.freeze 104 end
returns whether the response has been fully fetched.
# File lib/httpx/response.rb 96 def finished? 97 @finished 98 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 165 def form 166 decode(Transcoder::Form) 167 end
:nocov:
# File lib/httpx/response.rb 125 def inspect 126 "#<Response:#{object_id} " \ 127 "HTTP/#{version} " \ 128 "@status=#{@status} " \ 129 "@headers=#{@headers} " \ 130 "@body=#{@body.bytesize}>" 131 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 159 def json(*args) 160 decode(Transcoder::JSON, *args) 161 end
merges headers defined in h
into the response headers.
# File lib/httpx/response.rb 78 def merge_headers(h) 79 @headers = @headers.merge(h) 80 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 149 def raise_for_status 150 return self unless (err = error) 151 152 raise err 153 end
# File lib/httpx/response.rb 169 def xml 170 # TODO: remove at next major version. 171 warn "DEPRECATION WARNING: calling `.#{__method__}` on plain HTTPX responses is deprecated. " \ 172 "Use HTTPX.plugin(:xml) sessions and call `.#{__method__}` in its responses instead." 173 require "httpx/plugins/xml" 174 decode(Plugins::XML::Transcoder) 175 end