天天看點

Http Body 的四種格式Http Body 的四種格式

Http Body 的四種格式

1、參考資料

http的請求體body的幾種資料格式

HTTP協定之multipart/form-data請求分析

2、Http Body 的四種格式

在 Postman 的請求體中有這樣四種格式:

form-data

x-www-from-urlencoded

raw

binary

,那麼這四種格式分别代表什麼呢?

Http Body 的四種格式Http Body 的四種格式

2.1、form-data

form-data

的作用

form-data

就是 Http 協定中的

multipart/form-data

,表示以表單形式送出,它會将表單的資料處理為一條消息,以标簽為單元,用分隔符分開。既可以上傳鍵值對,也可以上傳檔案。

使用

form-data

傳輸字元串鍵值對

在 Postman 選擇 Body 的格式為

form-data

Http Body 的四種格式Http Body 的四種格式

Content-Type

字段的值為

multipart/form-data

,并且使用

boundary

作為邊界來劃分每對

key-value

鍵值對

Http Body 的四種格式Http Body 的四種格式
使用

form-data

傳輸檔案

如何在 Postman 中傳輸檔案?在 Postman 選擇 Body 的格式為

form-data

,輸入 Key 的時候選擇【File】即可上傳檔案

Http Body 的四種格式Http Body 的四種格式

Content-Type

字段的值為

multipart/form-data

,并且使用

boundary

作為邊界來劃分每對

key-value

鍵值對(檔案和檔案名也可以看作是

key-value

鍵值對的關系)

Http Body 的四種格式Http Body 的四種格式
使用

form-data

同時傳輸字元串鍵值對和檔案

在 Postman 選擇 Body 的格式為

form-data

,添加字元串鍵值對和檔案

Http Body 的四種格式Http Body 的四種格式

呐,其實傳輸檔案和傳輸字元串鍵值對也沒啥差別嘛~~~

Http Body 的四種格式Http Body 的四種格式

2.2、x-www-from-urlencoded

x-www-from-urlencoded

的作用

application/x-www-from-urlencoded

會将表單内的資料轉換為鍵值對,比如

name=oneby&hobby=coding

示範

x-www-from-urlencoded

在 Postman 選擇 Body 的格式為

x-www-from-urlencoded

Http Body 的四種格式Http Body 的四種格式

Content-Type

字段的值為

application/x-www-form-urlencoded

,表單送出的參數以

key-value

鍵值對的形式被封裝到請求體中

Http Body 的四種格式Http Body 的四種格式

2.3、raw

raw

的作用

可以上傳任意格式的文本

  1. 選擇

    text

    ,則請求頭是:

    text/plain

  2. 選擇

    javascript

    ,則請求頭是:

    application/javascript

  3. 選擇

    json

    ,則請求頭是:

    application/json

    (如果想以

    json

    格式傳參,就用

    raw+json

    就行了)
  4. 選擇

    html

    ,則請求頭是:

    text/html

  5. 選擇

    application/xml

    ,則請求頭是:

    application/xml

Http Body 的四種格式Http Body 的四種格式
示範

raw

傳輸

json

字元串

在 Postman 選擇 Body 的格式為

raw

,并且選擇

application/json

Http Body 的四種格式Http Body 的四種格式

Content-Type

字段的值為

application/json

,請求體内容就是我們填入的 json 字元串

Http Body 的四種格式Http Body 的四種格式

2.4、binary

binary

的作用

binary

通常用來上傳檔案,由于沒有鍵值,是以,一次隻能上傳一個檔案(一般用的不多)

示範

binary

傳輸單個檔案

在 Postman 選擇 Body 的格式為

binary

,并且選擇要上傳的檔案

Http Body 的四種格式Http Body 的四種格式

Content-Type

字段的值為

application/json

,隻有傳輸一個檔案,是以請求體内容就是檔案内容

Http Body 的四種格式Http Body 的四種格式

繼續閱讀