java 发表于 2017-10-26 18:24:02

Http POST请求数据提交格式

本帖最后由 java 于 2017-10-26 18:29 编辑

POST 提交数据方案,包含了 Content-Type 和消息主体编码方式两部分。


application/x-www-form-urlencoded最基本的form表单结构,用于传递字符参数的键值对,请求结构如下POST /user/login HTTP/1.1
Host: 192.168.134.15:8880
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 3473a66d-9642-e7a3-768d-4a5684d0c98b

username=admin&password=123456请求头中的Content-Type设置为application/x-www-form-urlencoded; 提交的的数据,请求body中按照 key1=value1&key2=value2 进行编码,key和value都要进行urlEncode;
java spring 接收方法:@RequestParam(value = "password") String password,
                        @RequestParam(value = "remember", required = false, defaultValue = "false") boolean remember,
multipart/form-data这是上传文件时,最常见的数据提交方式POSTHTTP/1.1
Host: www.demo.com
Cache-Control: no-cache
Postman-Token: 679d816d-8757-14fd-57f2-fbc2518dddd9
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="key"

value
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="testKey"

testValue
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="imgFile"; filename="no-file"
Content-Type: application/octet-stream


<data in here>
------WebKitFormBoundary7MA4YWxkTrZu0gW--首先请求头中的Content-Type 是multipart/form-data; 并且会随机生成一个boundary, 用于区分请求body中的各个数据; 每个数据以 --boundary 开始, 紧接着换行,下面是内容描述信息, 接着换2行, 接着是数据; 然后以 --boundary-- 结尾, 最后换行;每个换行都是 \r\n ;
java spring 接收方法:@RequestParam("txtFile") MultipartFile txtFile, @RequestParam("providerId") Integer ProviderId
application/jsonPOST http://www.example.com HTTP/1.1

Content-Type: application/json;charset=utf-8

{"title":"test","sub":}这种方案,可以方便的提交复杂的结构化数据,特别适合 RESTful 的接口。

java spring 接收方法:
@RequestBody AdminRolePara adminRolePara
text/xmltext/plain



页: [1]
查看完整版本: Http POST请求数据提交格式