Get the values of body to variables

霸气de小男生 提交于 2019-12-11 07:47:03

问题


I have a scenario where i need to pass the body values to an environment variable and use those in another API. In Postman

Below is the body,

{
  "firstName" : "Firstname",
  "lastName" : "lastname",
  "email" : "{{timestamp}}@test.com",
  "password" : "{{timestamp}}",
  "country" : 8l16
 }

Below is the Pre-req script,

  postman.setEnvironmentVariable("timestamp", (new 
  Date).getTime());
  // I have copied the Bodyand paste it in a variable called Obj in 
   Pre-req
 // Then i used the below script to get the body
  pm.environment.set("rawBody", JSON.stringify(obj));

But the environmental values of timestamp , email and password is coming as below. The timestamp value is correct and other two are wrong.

 timestamp = 1566076106769
 email = {{timestamp}}@test.com
  password = {{timestamp}}

The timestamp value is not getting substituted in email and password,i want the environmental variable value to set as,

Expected values,

 email = 1566076106769@test.com
 password = 1566076106769

So how can i assign the body element value to an environment/global variable to use in another API call?


回答1:


Easy. You have set the environmental variable but never got it. "{ }" doesn't work in code of Tests and Pre-request Script. Make it like this:

const timestamp = pm.environment.get('timestamp');
email = `${timestamp} @test.com`; 
password = timestamp;


来源:https://stackoverflow.com/questions/57541838/get-the-values-of-body-to-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!