Response Handling

The response object (if you’re in the documentation, look for HTTPX::Response) is the result after you perform the request.

The core implementation supports a minimal number of methods. The main ones are:

#status (Integer)

returns an Integer ranging from 100 to 599.

#headers (HTTPX::Headers)

returns an headers object. It has a minimal API to allow lookups, but it can also be turned into an Hash.

It will downcase all key lookups , so doing:

response.headers["content-type"]

or

response.headers["Content-Type"]

will return the same.

#body (HTTPX::Response::Body)

The body is a buffer where the HTTP response body lies. As all buffers, it has to be #close‘d when not anymore needed.

The buffer will be a temporary file if the body goes beyond a certain number of bytes.

One can start consuming the body by calling:

  • #to_s: returns the body as a String.
  • #each: yields the body in chunks of fixed size.

The above will close the buffer afterwards (and can therefore be used only once).

Other methods you can use to interact with the body:

  • #read: you can use this method to directly consume the buffer (don’t forget to close it when not needed anymore)
  • #copy_to: copies to buffer to another IO stream (great when you want to flush the response body into a file, for example):
response = HTTPX.get("https://companyfiles/someimage.jpg")
response.copy_to(File.open("/my/files/location"))

Next: Plugins