post類型的body中可以存放任意的內容格式,瀏覽器可以根據請求頭中指定的content-type類型對請求體進行解析。下面介紹postman如何對四種典型的請求體進行模擬。
一 form-data
即multipart/form-data,它將表單的數據組織成Key-Value形式,用分隔符boundary(boundary可任意設置)處理成一條消息。由於有boundary隔離,所以既可以上傳文件, 也可以上傳參數。
01
02
03
04
05
06
07
08
09
10
11
12
13
|
POST HTTP
/
1.1
Host
:
test.app.com
Cache
-
Control
:
no
-
cache
Postman
-
Token
:
59227787
-
c
438
-361
d
-
fbe
1
-75
feeb
78047
e
Content
-
Type
:
multipart
/
form
-
data
; boundary
=
----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content
-
Disposition
:
form
-
data
;
name
=
"filekey"
; filename
=
""
Content
-
Type
:
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content
-
Disposition
:
form
-
data
;
name
=
"textkey"
tttttt
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
請求體中的boundary參數指定的就是分隔體,可以看到請求內容被分為了兩段,第一段對應filekey,第二段對應textkey。
二 x-www-form-urlencoded
即application/x-www-from-urlencoded,將表單內的數據轉換為Key-Value。
1
2
3
4
5
6
7
|
POST HTTP
/
1.1
Host
:
test.app.com
Content
-
Type
:
application
/
x
-
www
-
form
-
urlencoded
Cache
-
Control
:
no
-
cache
Postman
-
Token
:
e
00
dbaf
5
-15
e
8
-3667
-6
fc
5
-48
ee
3
cc
89758
key
1
=
value
1
&
key
2
=
value
2
|
三 raw
可以上傳任意格式的【文本】,可以上傳text、json、xml、html等。比如json
1
2
3
4
5
6
7
|
POST HTTP
/
1.1
Host
:
test.app.com
Content
-
Type
:
application
/
json
Cache
-
Control
:
no
-
cache
Postman
-
Token
:
05
a
064
d
2
-
fa
79
-10
c
0
-
caba
-15
ca
5
d
1
a
940
f
{
"key1"
:
"value1"
,
"key2"
:
"value2"
}
|
四 binary
即Content-Type:application/octet-stream,只可以上傳二進制數據,通常用來上傳文件。由於沒有鍵值,所以一次只能上傳一個文件。
1
2
3
4
5
6
|
POST HTTP
/
1.1
Host
:
test.app.com
Cache
-
Control
:
no
-
cache
Postman
-
Token
:
5
ad
66
f
08
-6
faa
-
aba
0
-744
a
-
ca
958
b
1
a
0
fc
2
undefined
|
五 multipart/form-data與x-www-form-urlencoded區別
html中的form 表單有兩種:application/x-www-form-urlencoded和multipart/form-data。application/x-www-form-urlencoded是默認的MIME內容編碼類型,
它在傳輸比較大的二進制或者文本數據時效率極低。
multipart/form-data:既可以上傳文件等二進制數據,也可以上傳表單鍵值對,只是最后會轉化為一條信息。當設置multipart/form-data,http會忽略 contentType 屬性。
x-www-form-urlencoded:只能上傳鍵值對,不能用於文件上傳
更多免費技術資料可關注:annalin1203