Skip to content
Tony Arcieri edited this page Mar 7, 2016 · 15 revisions

When we make an HTTP request, we obtain an HTTP::Response object:

>> response = HTTP.get('https://www.google.com')
=> #<HTTP/1.0 200 OK @headers={"Content-Type"=>"text/html; charset=UTF-8", "Date"=>"Fri, ...>

There are several methods of interest we can call on the response (see also the YARD documentation):

  • #body: (returns HTTP::Body) an object representing the body. Can be used for streaming
  • #code: (returns Fixnum) the HTTP status code of the response
  • #content_type: (returns HTTP::ContentType) the content type of the response
  • #cookies: (returns HTTP::CookieJar) a cookie jar containing all of the response cookies for a request
  • #flush: (returns self) reads and discards the response body. Useful for persistent connections
  • #parse: (return value depends on MIME type) parse the body using a parser defined for the #content_type
  • #to_s: (returns String) quickly obtain the response body as a string
Clone this wiki locally