本文簡要介紹ruby語言中 Net::HTTP.post
的用法。
用法
post(path, data, initheader = nil, dest = nil) { |body_segment| ... }
將 data
(必須是 String
)發布到 path
。 header
必須是 Hash
,例如 { ‘Accept’ => ‘/’, ... }。
此方法返回一個 Net::HTTPResponse
對象。
如果使用塊調用,則在從套接字讀取時依次將實體主體的每個片段生成為字符串。請注意,在這種情況下,返回的響應對象將不包含(有意義的)正文。
dest
參數已過時。它仍然有效,但您不能使用它。
此方法從不引發異常。
response = http.post('/cgi-bin/search.rb', 'query=foo')
# using block
File.open('result.txt', 'w') {|f|
http.post('/cgi-bin/search.rb', 'query=foo') do |str|
f.write str
end
}
您應該為 POST 設置 Content-Type: 標頭字段。如果沒有給出Content-Type: 字段,此方法默認使用“application/x-www-form-urlencoded”。
相關用法
- Ruby HTTP.post_form用法及代碼示例
- Ruby HTTP.post用法及代碼示例
- Ruby HTTP.request_head用法及代碼示例
- Ruby HTTP.origin用法及代碼示例
- Ruby HTTP.set_debug_output用法及代碼示例
- Ruby HTTP.request_uri用法及代碼示例
- Ruby HTTP.request_post用法及代碼示例
- Ruby HTTP.send_request用法及代碼示例
- Ruby HTTP.request_get用法及代碼示例
- Ruby HTTP.head用法及代碼示例
- Ruby HTTP.get用法及代碼示例
- Ruby HTTP.authority用法及代碼示例
- Ruby HTTP.get_print用法及代碼示例
- Ruby HTTP.build用法及代碼示例
- Ruby HTTP.get_response用法及代碼示例
- Ruby HTTPResponse類用法及代碼示例
- Ruby HTTPHeader.to_hash用法及代碼示例
- Ruby HTTPHeader.add_field用法及代碼示例
- Ruby HTTPHeader.each_header用法及代碼示例
- Ruby HTTPResponse.read_body用法及代碼示例
- Ruby HTTPHeader.set_form_data用法及代碼示例
- Ruby HTTPHeader.get_fields用法及代碼示例
- Ruby HTTPHeader.set_form用法及代碼示例
- Ruby HTTPResponse.body用法及代碼示例
- Ruby HTTP類用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 HTTP.post。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。