class method
Net::HTTPSession.post
post(url, data, header = nil)
lib/net/http.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/net/http.rb, line 857
def HTTP.post(url, data, header = nil)
start(url.hostname, url.port,
:use_ssl => url.scheme == 'https' ) {|http|
http.post(url, data, header)
}
end
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:
-
Net::HTTP::Post: request class for HTTP methodPOST. -
Net::HTTP#post: convenience method for HTTP methodPOST.