Postman 中标头的默认预设

Jax*_*axx 10 rest postman

在 Postman 中手动测试请求时,有没有办法定义一些用于每个请求的默认标头集(无需在发送每个请求之前手动选择预设)?即有没有办法定义要使用的默认预设?

另外,当我点击响应中的某些链接(通过单击它)并发送对此链接的请求时,原始请求中的标头会丢失。有没有办法自动保存它们?

提前致谢

Muk*_*sal 6

没有直接的方法可以做到这一点,但可以通过集合级别的预请求脚本来完成。脚步-

  1. 在邮递员中创建一个集合,并将所有需要公共标头的请求放入其中。
  2. 编辑邮递员集合。

在此输入图像描述

  1. 将其写入您的预请求脚本部分。

var Header = require('postman-collection').Header;
pm.request.headers.add(new Header("foo:foo"))
pm.request.headers.add(new Header("bar:bar"))
Run Code Online (Sandbox Code Playgroud)

  • 你也可以这样做... `pm.request.headers.add({key: "foo", value: "bar"})` - 不需要 `require` 语句。 (5认同)

小智 5

从 7.0.9 开始- 您现在应该能够在预请求脚本中执行此操作。

您可以使用以下语法:

pm.request.headers.add({key: 'header_name', value: 'header_value' })

pm.request.headers.upsert({key: 'header_name', value: 'header_value' })

pm.request.headers.remove('header_name')
Run Code Online (Sandbox Code Playgroud)

提示:分叉此集合以查看它如何直接在 Postman 中工作: https://www.postman.com/postman/workspace/postman-answers/collection/9215231-ef055751-7385-45b4-a6f9-91bbd1c47fa5 ?ctx=documentation