Postman 中 form-data、x-www-form-urlencoded 和 raw 之間的區別
一、概述
Postman 提供了多種方式來與具有不同類型正文參數的 API 或服務器請求進行交互。這些代表通過 HTTP 請求向 API 發送數據的不同方式。
在本教程中,我們將探討在請求正文中使用form-data
、 x-www-form-urlencoded
和raw
之間的區別。
2. form-data
Form-data
表示從網站表單發送到 API 的數據,作為multipart/form-data
的一部分。 Postman 中的form-data
選項模擬在網站上填寫表單並提交。我們可以編輯表單數據,讓他通過轉換數據中的鍵值編輯器來設置不同的鍵/值對。
這也可以用於將文件附加到密鑰。但是,應該注意的是,使用 HTML5 會限製文件不在任何歷史記錄或集合中。因此,我們必須在發送請求正文時再次選擇文件。此外,Postman 不支持上傳多個文件及其內容類型。
請注意,Postman 將保留文件路徑以供後續使用,即,當我們重複進行 API 調用將相同的文件發送到服務器時。這有助於運行具有文件上傳的多個請求的集合。
讓我們看看在 Postman 中使用form-data
是什麼樣子的:
3. x-www-form-urlencoded
URL 編碼數據將編碼數據發送到服務器,並使用與 URL 參數相同的編碼。要使用它,我們需要在他們的請求正文中選擇x-www-form-urlencoded
選項卡。我們需要輸入鍵值對來將請求正文發送到服務器,Postman 會在發送之前對所需的數據進行編碼。 Postman 對鍵和值進行編碼。
注意它不能用於編碼文件,所以我們需要自己手動進行。但是,它只能對請求正文數據或 URL 參數進行編碼。
這也稱為默認內容類型。使用此內容類型提交的所有表單都遵循以下編碼模式:
- 控件名稱和值被轉義——所有空格字符將被“+”符號替換,保留字符遵循 RFC 17.38 符號。
- 等號,即“=”用於分隔鍵和值,鍵/值對使用“&”來分隔。
我們來看看 Postman 中的x-www-form-urlencoded
選項卡:
4. raw
顧名思義, raw
數據可以包含任何內容。 Postman 不會接觸raw
字符串或對其進行任何類型的更改。除了替換定義的環境變量外,添加到raw
編輯器中的字符串不會被修改。這個編輯器允許我們設置 Postman 支持的不同格式樣式以及需要與raw
正文一起發送的正確標題。支持以下類型:
- 文本
- Javascript
- JSON
- HTML
- XML
我們還可以將這些 Content-Type 手動設置到我們的請求正文中:
5. form-data
, x-www-form-urlencoded
和raw
的區別
我們可以使用W3C 委員會定義的不同形式的數據內容類型。他們為通過網絡層發送數據定義了多種格式。其中包括form-data
、x-www-form- urlencoded
和raw
數據。默認情況下,我們可以使用x-www-form-urlencoded
格式以簡單文本/ASCII 格式發送數據。
但是,使用x-www-form-urlencoded
數據類型有數據限制。因此,我們可以使用form-data
向服務器發送大的二進製或非 ASCII 文本。
顧名思義, raw
數據類型將任何純文本或 JSON 發送到服務器。它支持多種內容類型,與其他數據類型相比,Postman 將發送原始數據而無需任何修改。
我們可以使用raw
數據類型在請求正文中發送任何類型的數據。這還包括發送可以在服務器端執行的 Javascript 函數。我們可以在 Javascript 選項下發送腳本。 raw
數據類型還支持 HTML 和 XML 等標記語言。當前端沒有邏輯並且我們需要使用整個 HTML/XML 頁面時,這會很有幫助。
六,結論
在本文中,我們看到了 Postman 支持的一些請求正文數據類型。
我們還概述了 Postman 中form-data
、 x-www-form-urlencoded
和raw
之間的區別。但是,我們只了解了請求的基礎知識。您瀏覽Postman 在線文檔以更深入地了解這些請求正文類型。