【补充】POST请求的数据格式

协议规定POST提交的数据必须放在请求体中,但协议并没有规定数据必须使用什么编码方式。而常用的数据编码方式有:
https://www.runoob.com/http/http-content-type.html
列举几种最常见的数据格式:

  • Content-Type: application/x-www-form-urlencoded 数据被编码为名称/值对,默认类型;
  • Content-Type: multipart/form-data 数据被编码为一条消息,一般用于文件上传;
  • Content-Type:application/octet-stream 提交二进制数据,如果用于文件上传,只能上传一个文件
  • Content-Type:application/json 提交json数据

1、多个文件上传

File filel = new File( pathname: "C:\\Users\\Administrator\\Desktop\\1.txt");
File file2 = new File( pathname: "C:\\Users\\Administrator\\Desktop\\2.txt");
MultipartBody multipartBody = new MultipartBody.Builder()
.addFormDataPart(name:"file1",filel.getName(), RequestBody.create(filel,Mediatype.parse(("text/plain"))
.addFormDataPart(name: "file2",file2.getName(), RequestBody.create(file2,MediaType.parse(("text/plain"))
.addFormDataPart(name:"a",value: "1")
.build();
Request request = new Request.Builder().url("https://www.httpbin.org/post").post(multipartBody).build

2、Json上传

RequestBody requestBody = RequestBody.create("{\"a\":1,\"b\":2}", Mediatype.porse(""application/json"))
Request request = new Request.Builder().url("https://www.httpbin.org/post").post(requestBody).build();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贴纸。。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值