-
Notifications
You must be signed in to change notification settings - Fork 321
Passing Parameters
No GUI edited this page Apr 7, 2018
·
10 revisions
Use the :params
option to add query string parameters to requests:
HTTP.get("http://example.com/resource", :params => {:foo => "bar"})
Use the :form
option to pass data serialized as form encoded:
HTTP.post("http://example.com/resource", :form => {:foo => "42"})
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" })
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")
})
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"))