Skip to content
Marlin W. Pierce edited this page Jan 15, 2020 · 15 revisions

Require the gem in your project:

require 'http'

And make a GET request:

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

We now have a HTTP::Response object for the given request. We can obtain the body by calling #to_s on it:

>> response.to_s
=> "<html><head><meta http-equiv=\"content-type\" content=..."

Or get the response status code by calling #code:

>> response.code
=> 200

Many other HTTP methods are available as well:

>> response = HTTP.post('https://restapi.com/objects')
>> response = HTTP.put('https://restapi.com/put')
>> response = HTTP.patch('https://restapi.com/put')
>> response = HTTP.delete('https://restapi.com/put')
>> response = HTTP.head('https://restapi.com/put')

TLS/SSL options

TLS/SSL options are passed using the :ssl key in options, for example

HTTP.post("...",
          ssl: {verify_mode: OpenSSL::SSL::VERIFY_PEER,
                ca_file: "/usr/local/etc/trusted-certificates.pem"})

See OpenSSL::SSL::SSLContext for more information.

Supported HTTP methods

The following HTTP methods are available in this library:

RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1

  • #options
  • #get
  • #head
  • #post
  • #put
  • #delete
  • #trace
  • #connect

RFC 2518: HTTP Extensions for Distributed Authoring -- WEBDAV

  • #propfind
  • #proppatch
  • #mkcol
  • #copy
  • #move
  • #lock
  • #unlock

RFC 3648: WebDAV Ordered Collections Protocol

  • #orderpatch

RFC 3744: WebDAV Access Control Protocol

  • #acl

draft-dusseault-http-patch: PATCH Method for HTTP

  • #patch

draft-reschke-webdav-search: WebDAV Search

  • #search

See the METHODS constant in requests.rb for the full list of supported HTTP methods:

https://github.com/httprb/http/blob/master/lib/http/request.rb

See also