class Net::HTTPRequest
这个类是 Net::HTTP 请求类的基类。该类不应直接使用;而是应该使用其子类,如下所列。
创建请求¶ ↑
可以使用 URI
或字符串主机名来创建请求对象
require 'net/http' uri = URI('https://jsonplaceholder.typicode.com/') req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET> req = Net::HTTP::Get.new(uri.hostname) # => #<Net::HTTP::Get GET>
以及任何子类
req = Net::HTTP::Head.new(uri) # => #<Net::HTTP::Head HEAD> req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST> req = Net::HTTP::Put.new(uri) # => #<Net::HTTP::Put PUT> # ...
新实例适合作为 Net::HTTP#request
的参数使用。
请求头¶ ↑
默认情况下,新的请求对象具有以下标头字段
req.to_hash # => {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"], "accept"=>["*/*"], "user-agent"=>["Ruby"], "host"=>["jsonplaceholder.typicode.com"]}
请参阅
您可以添加标头或覆盖默认标头
# res = Net::HTTP::Get.new(uri, {'foo' => '0', 'bar' => '1'})
该类(及其子类)还(间接)包含模块 Net::HTTPHeader
,该模块提供对其 设置标头的方法的访问。
请求子类¶ ↑
HTTP 请求的子类
WebDAV 请求的子类
公共类方法
源代码
# File lib/net/http/request.rb, line 82 def initialize(path, initheader = nil) super self.class::METHOD, self.class::REQUEST_HAS_BODY, self.class::RESPONSE_HAS_BODY, path, initheader end
为 path
创建 HTTP 请求对象。
initheader
是要使用的默认标头。 Net::HTTP
会添加 Accept-Encoding 以启用响应正文的压缩,除非在 initheader
中提供了 Accept-Encoding 或 Range
。
调用父类方法
BasicObject::new