postman - Postman pre-request script iterate over request body JSON
问题描述
I am sending raw POST request with application/json data to server in Postman. I need to work with this JSON object and append some data in pre-request script. However I can only find how to access environmental variables, not request body. Anybody knows, please? Thanks!
解决方案
"I can only find how to access environmental variables, not request body"
You can access request body in Pre-request Script via pm.request.body
.
Unfortunately, you cannot change it through script (at least not supported in Jul. 2018). Please check this thread for some previous discussion.
However, there is a workaround: you can make the whole request body use environment variable, such as {{reqBody}}
, and edit that variable in Pre-request Script panel. For example:
var defaultReqBody = {
a: 42
};
//Edit defaultReqBody ...
pm.environment.set("reqBody", JSON.stringify(defaultReqBody));
推荐阅读
- html - 在我的 li 项下方添加文本
- c# - 调用HttpClient PostAsync方法上传图片时如何解决HttpRequestException
- jenkins - 如何在 Groovy 脚本中获取 Jenkins 构建参数?
- jquery - mvc- jquery - json 编码属性搜索
- css - 我有 2 列。我想根据内容调整一个,另一个以占用剩余空间
- mongodb - KeyError:从 MongoDB 同步数据时在 Elastic2DocManager 上“找到”
- django - 使用附加关键字参数时如何测试 DRF 序列化程序?
- jquery - 使用 jquery 将 iframe 内容导出为 pdf
- python - 具有嵌套数组的 Numpy 点积
- scala - Scala宏:根据Config生成类方法