postman模拟post请求的四种请求体

678 阅读2分钟

post类型的body中可以存放任意的内容格式,浏览器可以根据请求头中指定的content-type类型对请求体进行解析。下面介绍postman如何对四种典型的请求体进行模拟。
一 form-data
即multipart/form-data,它将表单的数据组织成Key-Value形式,用分隔符boundary(boundary可任意设置)处理成一条消息。由于有boundary隔离,所以既可以上传文件, 也可以上传参数。

[AppleScript] 纯文本查看 复制代码
?
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-c438-361d-fbe1-75feeb78047e
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: e00dbaf5-15e8-3667-6fc5-48ee3cc89758

key1=value1&key2=value2

三 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: 05a064d2-fa79-10c0-caba-15ca5d1a940f

{"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: 5ad66f08-6faa-aba0-744a-ca958b1a0fc2

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:只能上传键值对,不能用于文件上传