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