Skip to content
instance method

Net::HTTP#request

(::Net::HTTPRequest req, ?::String? body) ?{ (::Net::HTTPResponse response) -> void } -> ::Net::HTTPResponse
request(req, body = nil) { |response| ... }

Sends the given request req to the server; forms the response into a Net::HTTPResponse object.

The given req must be an instance of a subclass of Net::HTTPRequest. Argument body should be given only if needed for the request.

With no block given, returns the response object:

Example
http = Net::HTTP.new(hostname)

req = Net::HTTP::Get.new('/todos/1')
http.request(req)
# => #<Net::HTTPOK 200 OK readbody=true>

req = Net::HTTP::Post.new('/todos')
http.request(req, 'xyzzy')
# => #<Net::HTTPCreated 201 Created readbody=true>

With a block given, calls the block with the response and returns the response:

Example
req = Net::HTTP::Get.new('/todos/1')
http.request(req) do |res|
  p res
end # => #<Net::HTTPOK 200 OK readbody=true>

Output:

Example
#<Net::HTTPOK 200 OK readbody=false>
See also #read_timeout= #request_get #put #request_head also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0