instance method
Net::HTTPSession#post
post(path, data, initheader = nil) {|res| ... }
lib/net/http.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/net/http.rb, line 2071
def post(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
send_entity(path, data, initheader, dest, Post, &block)
end
Sends a POST request to the server; returns an instance of a subclass of Net::HTTPResponse.
The request is based on the Net::HTTP::Post object created from string path, string data, and initial headers hash initheader.
With a block given, calls the block with the response body:
Example
data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}' http = Net::HTTP.new(hostname) http.post('/todos', data) do |res| p res end # => #<Net::HTTPCreated 201 Created readbody=true>
Output:
Example
"{\n \"{\\\"userId\\\": 1, \\\"id\\\": 1, \\\"title\\\": \\\"delectus aut autem\\\", \\\"completed\\\": false}\": \"\",\n \"id\": 201\n}"
With no block given, simply returns the response object:
Example
http.post('/todos', data) # => #<Net::HTTPCreated 201 Created readbody=true>
Related:
-
Net::HTTP::Post: request class for HTTP method POST. -
Net::HTTP.post: sends POST request, returns response body.