Skip to content
Tony Arcieri edited this page Feb 14, 2016 · 10 revisions

Query string parameters

Use the :params option to add query string parameters to requests:

HTTP.get("http://example.com/resource", :params => {:foo => "bar"})

Form data

Use the :form option to pass data serialized as form encoded:

HTTP.post("http://example.com/resource", :form => {:foo => "42"})

Request bodies

The :body option allows posting a raw request body:

HTTP.post("http://example.com/resource", :body => "foo=42&bar=baz")

Request bodies can be a String or any Enumerable object. Enumerable objects can be used for streaming request bodies.

The :json option allows serializing Ruby objects as JSON:

HTTP.post("http://example.com/resource", :json => { :foo => "42" })

File uploads (via form data)

To upload files as if form data, construct the form as follows:

HTTP.post("http://examplc.com/resource", :form => {
  :username => "ixti",
  :avatar   => HTTP::FormData::File.new("/home/ixit/avatar.png")
})

See also