當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby HTTP.post用法及代碼示例

本文簡要介紹ruby語言中 Net::HTTP.post 的用法。

用法

post(path, data, initheader = nil, dest = nil) { |body_segment| ... }

data (必須是 String )發布到 pathheader 必須是 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-lang.org大神的英文原創作品 HTTP.post。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。