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


Ruby HTTPHeader.set_form用法及代碼示例

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

用法

set_form(params, enctype='application/x-www-form-urlencoded', formopt={})

Set 一個 HTML 表單數據集。

params

要設置的表單數據,應該是可枚舉的。請參閱下麵的更多細節。

enctype

用於對表單提交進行編碼的內容類型,應為 application/x-www-form-urlencoded 或 multipart/form-data。

formopt

一個選項哈希,支持以下選項:

:邊界

多部分消息的邊界。如果沒有給出,將使用隨機邊界。

:字符集

表單提交的字符集。非文件字段的所有字段名稱和值都應使用此字符集進行編碼。

每個參數項都應響應 each 並產生 2-3 個參數,或 2-3 個元素的數組。產生的論點應該是:

* The name of the field.
* The value of the field, it should be a String or a File or IO-like.
* An options hash, supporting the following options, only
  used for file uploads:
  :filename :: The name of the file to use.
  :content_type :: The content type of the uploaded file.

每個項目都是一個文件字段或一個普通字段。如果 value File 對象或 opt 哈希具有 :filename 鍵,則該項目被視為文件字段。

如果Transfer-Encoding 設置為分塊,這將使用分塊編碼發送請求。因為分塊編碼是 HTTP/1.1 的特性,所以在使用分塊編碼之前,您應該確認服務器支持 HTTP/1.1。

例子:

req.set_form([["q", "ruby"], ["lang", "en"]])

req.set_form({"f"=>File.open('/path/to/filename')},
             "multipart/form-data",
             charset: "UTF-8",
)

req.set_form([["f",
               File.open('/path/to/filename.bar'),
               {filename: "other-filename.foo"}
             ]],
             "multipart/form-data",
)

另請參閱 RFC 2388、RFC 2616、HTML 4.01 和 HTML5

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 HTTPHeader.set_form。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。