邮递员 - 如何将全局变量传递到 JSON 正文中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36307487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:21:41  来源:igfitidea点击:

Postman - How to pass a global variable into JSON body

jsonrestrequestpostman

提问by Dan Paschevici

I'm working with Postman right now and I have to do a lot of requests, and in the bodyI'm passing a JSON content. I would like to know if is there a way to pass the value of a global variable into JSON body. Thank you.

我现在正在和 Postman 一起工作,我必须做很多请求,而且body我正在传递一个 JSON 内容。我想知道是否有办法将全局变量的值传递到 JSON 正文中。谢谢你。

回答by RadamHussein

If using a raw JSON body:

如果使用原始 JSON 正文:

{
    "test key":"{{global variable}}"
}

On sending the request, the curly braces will be replaced with the value of the variable.

在发送请求时,花括号将替换为变量的值。

回答by Philipp Stauss

I think what you want to do is described here.

我想你想要做的就是这里描述的。

To use a variable you need to enclose the variable name with double curly braces – {{my_variable_name}}.

要使用变量,您需要将变量名用双花括号括起来 - {{my_variable_name}}。

回答by bartpie

Double curly bracket is working in header parameter, url or inside JSON body. Inside tests you should use globalsexample: {"url": globals.url}or {"url": globals["url"]}

双大括号在标头参数、url 或 JSON 正文中工作。内部测试您应该使用globals示例:{"url": globals.url}{"url": globals["url"]}

回答by KolonUK

Yep, double curly brackets is the way to go to achieve this - make sure you have the latest version of Postman (though if you are still running 2014 code when this feature was introduced, shame on you!)

是的,双大括号是实现这一目标的方法 - 确保您拥有最新版本的 Postman(不过,如果您在引入此功能时仍在运行 2014 年的代码,那您真丢脸!)

eg:

例如:

{
    "variable": "{{value}}"
}

See the second paragraph herein the Variables section of the documentation - it specifically mentions the request body.

见第二段这里在文档中的变量部分-它明确提到的request body

回答by Shailendra

You can pass

你可以通过

{
   "productId": {{**ProductID**}},
   "quantity": 1
}

Where ProductIDis your global variable name
in raw format JSON (application/json)

其中ProductID
原始格式 JSON (application/json) 中的全局变量名称