在预请求脚本中为 Postman 中的每个请求添加标头 [英] Add header to every request in Postman in pre-request script
本文介绍了在预请求脚本中为 Postman 中的每个请求添加标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用这个预请求脚本自动为我的整个集合中的每个请求添加一个标头:
I want to automatically add a header to every request in my whole collection using this pre-request script:
pm.request.headers.add({
'key': "myvar",
'value': pm.environment.get("myvar")
});
myvar 是一个环境变量.
不幸的是,它不起作用.我错过了什么吗?
Unfortunately, it doesn't work. Did I miss something?
推荐答案
对于那些在 postman ~ 7.10.0 上尝试的人,您可以在预请求脚本中以编程方式将标头添加到请求或集合中(into collection 将向集合内的所有请求添加标头.
For those who are trying it on postman ~ 7.10.0, you can add headers programmatically in a pre-request script, into the request or into the collection (into collection will add headers to all requests inside collection).
pm.request.headers.add({
// These keys appears when you set a header by hand. Just for fun they are here
disabled: false,
description:{
content: "DescriptionTest",
type: "text/plain"
},
// Your header, effectively
key: 'KeyTest',
name: 'NameTest',
// If you set a variable you can access it
// HeaderTest here has value="ValueHeaderTest"
value: pm.collectionVariables.get("HeaderTest")
});
代码片段生成器不会显示添加的标头:
The code snippet generator will not show the added header:
GET /get_info.php HTTP/1.1
Host: 192.168.15.25:8001
Content-type: application/json
User-Agent: PostmanRuntime/7.19.0
Accept: */*
Host: 192.168.15.25:8001
Accept-Encoding: gzip, deflate
Connection: keep-alive
但邮递员控制台会:
GET /get_info.php HTTP/1.1
Content-type: application/json
KeyTest: ValueHeaderTest
User-Agent: PostmanRuntime/7.19.0
Accept: */*
Host: 192.168.15.25:8001
Accept-Encoding: gzip, deflate
Connection: keep-alive
这篇关于在预请求脚本中为 Postman 中的每个请求添加标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文