class Net::HTTPGenericRequest
HTTPGenericRequest 是 Net::HTTPRequest 类的父类。
请勿直接使用此类;请使用 Net::HTTPRequest 的子类。
关于示例
此处的示例假设已加载 net/http(这也将加载 uri)
require 'net/http'
这里的许多代码示例都使用了这些示例网站
一些示例还假定存在这些变量
uri = URI('https://jsonplaceholder.typicode.com/') uri.freeze # Examples may not modify. hostname = uri.hostname # => "jsonplaceholder.typicode.com" path = uri.path # => "/" port = uri.port # => 443
因此,示例请求可以这样编写
Net::HTTP.get(uri) Net::HTTP.get(hostname, '/index.html') Net::HTTP.start(hostname) do |http| http.get('/todos/1') http.get('/todos/2') end
需要修改 URI 的示例首先复制 uri,然后修改副本
_uri = uri.dup _uri.path = '/todos/1'
属性
返回请求的字符串正文,如果没有则返回 nil
req = Net::HTTP::Post.new(uri) req.body # => nil req.body = '{"title": "foo","body": "bar","userId": 1}' req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
返回请求的正文流对象,如果没有则返回 nil
req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST> req.body_stream # => nil require 'stringio' req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8> req.body_stream # => #<StringIO:0x0000027d1e5affa8>
如果请求的头部 'Accept-Encoding' 已手动设置或删除(表明用户打算处理响应中的编码),则返回 false,否则返回 true
req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET> req['Accept-Encoding'] # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3" req.decode_content # => true req['Accept-Encoding'] = 'foo' req.decode_content # => false req.delete('Accept-Encoding') req.decode_content # => false
返回请求的字符串方法名
Net::HTTP::Get.new(uri).method # => "GET" Net::HTTP::Post.new(uri).method # => "POST"
返回请求的字符串路径
Net::HTTP::Get.new(uri).path # => "/" Net::HTTP::Post.new('example.com').path # => "example.com"
Public Instance Methods
Source
# File lib/net/http/generic_request.rb, line 176 def body=(str) @body = str @body_stream = nil @body_data = nil str end
设置请求的正文
req = Net::HTTP::Post.new(uri) req.body # => nil req.body = '{"title": "foo","body": "bar","userId": 1}' req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
Source
# File lib/net/http/generic_request.rb, line 201 def body_stream=(input) @body = nil @body_stream = input @body_data = nil input end
设置请求的正文流
req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST> req.body_stream # => nil require 'stringio' req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8> req.body_stream # => #<StringIO:0x0000027d1e5affa8>
Source
# File lib/net/http/generic_request.rb, line 98 def inspect "\#<#{self.class} #{@method}>" end
返回请求的字符串表示形式
Net::HTTP::Post.new(uri).inspect # => "#<Net::HTTP::Post POST>"
Source
# File lib/net/http/generic_request.rb, line 116 def pretty_print(q) q.object_group(self) { q.breakable q.text @method q.breakable q.text "path="; q.pp @path q.breakable q.text "headers="; q.pp to_hash } end
返回请求的字符串表示形式,包含 pp 的详细信息
require 'pp'
post = Net::HTTP::Post.new(uri)
post.inspect # => "#<Net::HTTP::Post POST>"
post.pretty_inspect
# => #<Net::HTTP::Post
POST
path="/"
headers={"accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
"accept" => ["*/*"],
"user-agent" => ["Ruby"],
"host" => ["www.ruby-lang.org"]}>
Source
# File lib/net/http/generic_request.rb, line 142 def request_body_permitted? @request_has_body end
返回请求是否允许有正文
Net::HTTP::Post.new(uri).request_body_permitted? # => true Net::HTTP::Get.new(uri).request_body_permitted? # => false
Source
# File lib/net/http/generic_request.rb, line 151 def response_body_permitted? @response_has_body end
返回响应是否允许有正文
Net::HTTP::Post.new(uri).response_body_permitted? # => true Net::HTTP::Head.new(uri).response_body_permitted? # => false