首页 > 解决方案 > 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!

标签: postmanpostman-pre-request-script

解决方案


"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));

推荐阅读