我想在Postman中参数化我的测试。这是POST请求的示例主体:
{ "entity_id": "{{entity_id}}", "text": data.comment_name }
entity_id是全局变量,它可以正常工作,但我想从JSON文件中设置text的值。data.comment_name不起作用,它将data.comment_name保存为文本变量。我想在collection runner中包含这个JSON文件和所有变量。我怎么能这么做呢?
entity_id
text
data.comment_name
jv2fixgn1#
如果你的数据文件看起来像这个JSON示例:
[ { "entity_id": 1, "comment_name": "This is my comment_name" } ]
要引用文件中的值,POST主体需要看起来像这样:
这就是你的例子,但你不需要在变量周围添加" "。如果值是一个数字,并且在请求正文中的值周围添加引号,这可能会导致问题,这将使其成为字符串,并可能导致错误的请求。
" "
pw9qyyiw2#
通过使用pre-request scripts,我们可以在请求运行之前通过执行JavaScript来更改变量值、参数、头和主体数据。Code tested in Postman-win64-8.12.4-Setup请求正文-原始
pre-request scripts
{ "locale": "en_US", "key" : "{{dynamic_key}}", "value" : "{{dynamic_val}}" }
Writing pre-request scripts
pm.collectionVariables.set("dynamic_key", "key1"); pm.collectionVariables.set("dynamic_val", "val1"); console.log('Add variable to JSON body', pm.request.body);
?id=1&type=new
/customer/:id
2条答案
按热度按时间jv2fixgn1#
如果你的数据文件看起来像这个JSON示例:
要引用文件中的值,POST主体需要看起来像这样:
这就是你的例子,但你不需要在变量周围添加
" "
。如果值是一个数字,并且在请求正文中的值周围添加引号,这可能会导致问题,这将使其成为字符串,并可能导致错误的请求。pw9qyyiw2#
预请求脚本Add variable to JSON body
通过使用
pre-request scripts
,我们可以在请求运行之前通过执行JavaScript来更改变量值、参数、头和主体数据。Code tested in Postman-win64-8.12.4-Setup
请求正文-原始
Writing pre-request scripts
?id=1&type=new
), 路径参数 *(/customer/:id
)]