Class: HTTPX::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Callbacks, ResponsePatternMatchExtensions
Defined in:
lib/httpx/response.rb

Overview

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

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

  • HTTPX::Request#uri

  • HTTPX::Request#peer_address

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).

Defined Under Namespace

Classes: Body, Buffer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResponsePatternMatchExtensions

#deconstruct, #deconstruct_keys

Methods included from Callbacks

#callbacks_for?, #emit, #on, #once

Constructor Details

#initialize(request, status, version, headers) ⇒ Response

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



66
67
68
69
70
71
72
73
74
75
# File 'lib/httpx/response.rb', line 66

def initialize(request, status, version, headers)
  @request = request
  @options = request.options
  @version = version
  @status = Integer(status)
  @headers = @options.headers_class.new(headers)
  @body = @options.response_body_class.new(self, @options)
  @finished = complete?
  @content_type = nil
end

Instance Attribute Details

#bodyObject (readonly)

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

  • HTTPX::Response::Body#to_s

  • HTTPX::Response::Body#to_str

  • HTTPX::Response::Body#read

  • HTTPX::Response::Body#copy_to

  • HTTPX::Response::Body#close



39
40
41
# File 'lib/httpx/response.rb', line 39

def body
  @body
end

#headersObject (readonly)

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



30
31
32
# File 'lib/httpx/response.rb', line 30

def headers
  @headers
end

#statusObject (readonly)

the HTTP response status code



27
28
29
# File 'lib/httpx/response.rb', line 27

def status
  @status
end

#versionObject (readonly)

The HTTP protocol version used to fetch the response.



42
43
44
# File 'lib/httpx/response.rb', line 42

def version
  @version
end

Instance Method Details

#<<(data) ⇒ Object

writes data chunk into the response body.



83
84
85
# File 'lib/httpx/response.rb', line 83

def <<(data)
  @body.write(data)
end

#bodyless?Boolean

returns whether the response contains body payload.

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/httpx/response.rb', line 107

def bodyless?
  @request.verb == "HEAD" ||
    @status < 200 || # informational response
    @status == 204 ||
    @status == 205 ||
    @status == 304 || begin
      content_length = @headers["content-length"]
      return false if content_length.nil?

      content_length == "0"
    end
end

#complete?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/httpx/response.rb', line 120

def complete?
  bodyless? || (@request.verb == "CONNECT" && @status == 200)
end

#content_typeObject

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"


91
92
93
# File 'lib/httpx/response.rb', line 91

def content_type
  @content_type ||= ContentType.new(@headers["content-type"])
end

#errorObject

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


139
140
141
142
143
# File 'lib/httpx/response.rb', line 139

def error
  return if @status < 400

  HTTPError.new(self)
end

#finish!Object

marks the response as finished, freezes the headers.



101
102
103
104
# File 'lib/httpx/response.rb', line 101

def finish!
  @finished = true
  @headers.freeze
end

#finished?Boolean

returns whether the response has been fully fetched.

Returns:

  • (Boolean)


96
97
98
# File 'lib/httpx/response.rb', line 96

def finished?
  @finished
end

#formObject

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



165
166
167
# File 'lib/httpx/response.rb', line 165

def form
  decode(Transcoder::Form)
end

#inspectObject

:nocov:



125
126
127
128
129
130
131
# File 'lib/httpx/response.rb', line 125

def inspect
  "#<Response:#{object_id} " \
    "HTTP/#{version} " \
    "@status=#{@status} " \
    "@headers=#{@headers} " \
    "@body=#{@body.bytesize}>"
end

#json(*args) ⇒ Object

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


159
160
161
# File 'lib/httpx/response.rb', line 159

def json(*args)
  decode(Transcoder::JSON, *args)
end

#merge_headers(h) ⇒ Object

merges headers defined in h into the response headers.



78
79
80
# File 'lib/httpx/response.rb', line 78

def merge_headers(h)
  @headers = @headers.merge(h)
end

#raise_for_statusObject

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


149
150
151
152
153
# File 'lib/httpx/response.rb', line 149

def raise_for_status
  return self unless (err = error)

  raise err
end

#xmlObject



169
170
171
172
173
174
175
# File 'lib/httpx/response.rb', line 169

def xml
  # TODO: remove at next major version.
  warn "DEPRECATION WARNING: calling `.#{__method__}` on plain HTTPX responses is deprecated. " \
       "Use HTTPX.plugin(:xml) sessions and call `.#{__method__}` in its responses instead."
  require "httpx/plugins/xml"
  decode(Plugins::XML::Transcoder)
end