来源:https://blog.csdn.net/m0_37556444/article/details/82845694

postman的几种参数格式

post类型的body中可以存放任意的内容格式,浏览器可以根据请求头中指定的content-type类型对请求体进行解析。下面介绍postman如何对四种典型的请求体进行模拟。

1. form-data
即multipart/form-data,它将表单的数据组织成Key-Value形式,用分隔符boundary(boundary可任意设置)处理成一条消息。
由于有boundary隔离,所以既可以上传文件,也可以上传参数。

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--
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

请求体中的boundary参数指定的就是分隔体,可以看到请求内容被分为了两段,第一段对应filekey,第二段对应textkey。

2. x-www-form-urlencoded
即application/x-www-from-urlencoded,将表单内的数据转换为Key-Value。

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-48ee3cc89758key1=value1&key2=value2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3. raw
可以上传任意格式的【文本】,可以上传text、json、xml、html等

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"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4. binary
即Content-Type:application/octet-stream,只可以上传二进制数据,通常用来上传文件。由于没有键值,所以一次只能上传一个文件。

POST  HTTP/1.1
Host: test.app.com
Cache-Control: no-cache
Postman-Token: 5ad66f08-6faa-aba0-744a-ca958b1a0fc2undefined
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

提醒:
multipart/form-data与x-www-form-urlencoded区别:
  html中的form 表单有两种:application/x-www-form-urlencoded和multipart/form-data。application/x-www-form-urlencoded是默认的MIME内容编码类型,它在传输比较大的二进制或者文本数据时效率极低。

MIME:
简单说,MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型。服务器会将它们发送的多媒体数据的类型告诉浏览器,而通知手段就是说明该多媒体数据的MIME类型,服务器将 MIME标志符放入传送的数据中来告诉浏览器使用哪种插件读取相关文件。

multipart/form-data:既可以上传文件等二进制数据,也可以上传表单键值对,只是最后会转化为一条信息。当设置multipart/form-data,http会忽略 contentType 属性。

x-www-form-urlencoded:只能上传键值对,不能用于文件上传。不同的field是用&区分开的。这两个类均实现了HttpEntity接口,使用如下:

public static String testUpload(String url) {String result = null;CloseableHttpClient httpclient = HttpClients.createDefault();HttpPost httppost = new HttpPost(url);try {FileBody bin = new FileBody(new File("F:\\image\\sendpix0.jpg"));StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build();httppost.setEntity(reqEntity);System.out.println("executing request " + httppost.getRequestLine());CloseableHttpResponse response = httpclient.execute(httppost);try {int statusCode = response.getStatusLine().getStatusCode();if (statusCode == HttpStatus.SC_OK) {result = EntityUtils.toString(response.getEntity(), "UTF-8");}} finally {response.close();httpclient.close();}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}return result;}public static String testParam(String url) {String result = null;CloseableHttpClient httpclient = HttpClients.createDefault();httpclient = HttpsHelper.newHttpsCloseableClient();HttpPost httpPost = new HttpPost(url);List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("key1", "value1"));params.add(new BasicNameValuePair("key2", "value2"));try {httpPost.setEntity(new UrlEncodedFormEntity(params));httpPost.setConfig(requestConfig);CloseableHttpResponse httpResp = httpclient.execute(httpPost);try {int statusCode = httpResp.getStatusLine().getStatusCode();if (statusCode == HttpStatus.SC_OK) {result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");}} finally {httpResp.close();httpclient.close();}} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}return result;}

postman模拟post请求的四种请求体相关推荐

  1. python写http post请求的四种请求体

    Web自动化测试(25)  HTTP 协议规定 POST 提交的数据必须放在消息主体(entity-body)中,但协议并没有规定数据必须使用什么编码方式.常见的四种编码方式如下:  1.applic ...

  2. python post body_python写http post请求的四种请求体

    HTTP 协议规定 POST 提交的数据必须放在消息主体(entity-body)中,但协议并没有规定数据必须使用什么编码方式.常见的四种编码方式如下: 1.application/x-www-for ...

  3. postman的四种请求:post,get,put,delete

    四种请求的详解 前言 一.get请求 二.post请求 三.put请求 四.delete请求 前言 之前在做测试的实习的时候用过postman做接口测试,但是以前懵懵懂懂,觉得那是开发才相关的东西,所 ...

  4. 接口测试中模拟post四种请求数据

    转自 作者:隋胖胖LoveFat 链接:https://www.jianshu.com/p/3b6d7aa2043a 来源:简书 一.背景介绍 在日常的接口测试工作中,模拟接口请求通常有两种方法,fi ...

  5. axios队列 vue_(十三 )Vue 封装axios(四种请求)及相关介绍

    Vue 封装axios(四种请求)及相关介绍 首先axios是基于promise的http库 promise是什么? 1.主要用于异步计算 2.可以将异步操作队列化,按照期望的顺序执行,返回符合预期的 ...

  6. java rest风格传参_SpringMVC的REST风格的四种请求方式总结

    一. 在HTTP 协议里面,四个表示操作方式的动词:GET.POST.PUT.DELETE. 它们分别对应四种基本操作: 1.GET ====== 获 取资源 2.POST ======新建资源 3. ...

  7. java的rest教程_[Java教程]SpringMVC的REST风格的四种请求方式

    [Java教程]SpringMVC的REST风格的四种请求方式 0 2017-08-28 22:00:25 一. 在HTTP 协议里面,四个表示操作方式的动词:GET.POST.PUT.DELETE. ...

  8. Flask框架中的四种请求勾子

    在客户端和服务器交互的过程中,有些准备工作或扫尾工作需要处理,比如: 在请求开始时,建立数据库连接:在请求开始时,根据需求进行权限校验:在请求结束时,指定数据的交互格式: 为了让每个视图函数避免编写重 ...

  9. curl 发送 POST 请求的四种方式

    使用 curl 发送 POST 请求的四种方式: application/x-www-form-urlencoded 使用实例 $ curl localhost:3000/api/basic -X P ...

最新文章

  1. 【响应式Web前端设计】!important的用法及作用
  2. Python 懂车帝车友圈--分析与实现
  3. 推荐: 五分钟搞懂Xen、KVM、Qemu间的关系和区别[转载]
  4. mysql数据库试题下载_MYSQL数据库2013-2014学年考试试卷
  5. [pytorch、学习] - 5.9 含并行连结的网络(GoogLeNet)
  6. 实验 5 编写、调试具有多个段的
  7. Android 系统性能优化(74)---如何减少lowmemory的发生几率
  8. 用神经网络例子讲解TF运行方式~人工智能入门编程例子讲解
  9. Pytorch——激活函数(Activation Function)
  10. TCP加速机制是如何加速的?
  11. BUAA OO 2019 第一单元作业总结
  12. ffplay的音视频同步分析
  13. 小米php架构图,小米商城基本框架部分
  14. Kindle Paperwhite安装PDF阅读器Koreader
  15. 《论文排版札记》part1 论文公式编号—WPS版
  16. win10硬盘锁怎么解除_win10系统如何解锁bitlocker的硬盘加密
  17. 【奇葩瑞萨-002】调教Renesas RX130独立看门狗
  18. 'Connection aborted.', BadStatusLine('\x15\x03\x01\x00\x02\x02\x16',)
  19. 由左右眼图片合成红蓝/红青立体图
  20. 尚硅谷在线教育十四:微信支付

热门文章

  1. mysql+挂马代码_转载:挂马代码大全
  2. 14_pytorch.where,pytorch.gather
  3. Docker容器间通讯,直接路由方式实现网络通讯
  4. 17_Android中Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数,传递参数的时候指定权限
  5. STL之stack,queue,优先队列
  6. Hibernate优化策略
  7. harbor登录验证_Harbor 源码浅析
  8. FCN训练自己的数据集及测试
  9. 编写一个函数,函数接收一个字符串,是由十六进制数组成的一组字符串,函数的功能是把接到的这组字符串转换成十进制数字.并将十进制数字返回
  10. Nginx搭建部署Web服务器并与NFS结合搭建负载均衡服务器