Skip to content
class method

Net::HTTPSession.post

post(url, data, header = nil)

Posts data to a host; returns a Net::HTTPResponse object.

Argument url must be a URL; argument data must be a string:

Example
_uri = uri.dup
_uri.path = '/posts'
data = '{"title": "foo", "body": "bar", "userId": 1}'
headers = {'content-type': 'application/json'}
res = Net::HTTP.post(_uri, data, headers) # => #<Net::HTTPCreated 201 Created readbody=true>
puts res.body

Output:

Example
{
  "title": "foo",
  "body": "bar",
  "userId": 1,
  "id": 101
}

Related: