Skip to content
No GUI edited this page Apr 7, 2018 · 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 String, Enumerable or IO-like object. Enumerable and IO-like 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 in multipart format, construct the form as follows:

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

File uploads (raw)

To upload files in raw format, pass the file as the request body:

HTTP.post("http://example.com/resource", body: File.open("/home/ixit/avatar.png"))

See also