Defines a HTTP response is handled internally, with a few properties exposed as attributes, implements (indirectly, via the body
) the IO write protocol to internally buffer payloads, 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 52 def initialize(request, status, version, headers) 53 @request = request 54 @options = request.options 55 @version = version 56 @status = Integer(status) 57 @headers = @options.headers_class.new(headers) 58 @body = @options.response_body_class.new(self, @options) 59 @finished = complete? 60 @content_type = nil 61 end
Public Instance methods
writes data
chunk into the response body.
# File lib/httpx/response.rb 69 def <<(data) 70 @body.write(data) 71 end
returns whether the response contains body payload.
# File lib/httpx/response.rb 92 def bodyless? 93 @request.verb == "HEAD" || 94 @status < 200 || # informational response 95 @status == 204 || 96 @status == 205 || 97 @status == 304 || begin 98 content_length = @headers["content-length"] 99 return false if content_length.nil? 100 101 content_length == "0" 102 end 103 end
# File lib/httpx/response.rb 105 def complete? 106 bodyless? || (@request.verb == "CONNECT" && @status == 200) 107 end
returns the response mime type, as per what’s declared in the content-type header.
response.content_type #=> "text/plain"
# File lib/httpx/response.rb 76 def content_type 77 @content_type ||= ContentType.new(@headers["content-type"]) 78 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 124 def error 125 return if @status < 400 126 127 HTTPError.new(self) 128 end
marks the response as finished, freezes the headers.
# File lib/httpx/response.rb 86 def finish! 87 @finished = true 88 @headers.freeze 89 end
returns whether the response has been fully fetched.
# File lib/httpx/response.rb 81 def finished? 82 @finished 83 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 150 def form 151 decode(Transcoder::Form) 152 end
:nocov:
# File lib/httpx/response.rb 110 def inspect 111 "#<Response:#{object_id} " \ 112 "HTTP/#{version} " \ 113 "@status=#{@status} " \ 114 "@headers=#{@headers} " \ 115 "@body=#{@body.bytesize}>" 116 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 144 def json(*args) 145 decode(Transcoder::JSON, *args) 146 end
merges headers defined in h
into the response headers.
# File lib/httpx/response.rb 64 def merge_headers(h) 65 @headers = @headers.merge(h) 66 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 134 def raise_for_status 135 return self unless (err = error) 136 137 raise err 138 end
decodes the response payload into a Nokogiri::XML::Node object if the payload is valid “application/xml” (requires the “nokogiri” gem).
# File lib/httpx/response.rb 156 def xml 157 decode(Transcoder::Xml) 158 end