1
votes

In a postman request json body, I want to set a variable to a variable.

This works fine for the built in $timestamp, But can I do it for other numerics?

It seems that postman always sends the variable as a string encoding.

For Example, in the pre-request script:

var lat = +pm.environment.get('lat');
lat = clat + 0.001;
console.log(lat);
pm.environment.set('lat',  Number.parseFloat(lat).toPrecision(5));

In the body:

{
  "unixTimeStamp":  {{$timestamp}}, //OK

  "lng":  145.123,   // OK

  "lat":  {{$lat}},   // Fails
}

This sends:

{
  "unixTimeStamp":  1521769729,

  "lng":  145.123,  

  "lat":  "-37",   // Web service rejects this field.
}

I believe that numeric is always encoded as a string.

1

1 Answers

2
votes

The $ in front of the variable name is only used In a few places for the built in dynamic variables. For example $timestamp, $guid and $randomInt.

Every value in the Environment file is stored as a string so you would need to parse this in some way to get the value that you require.

You can use parseInt(pm.environment.get('my_string') to convert a saved string "100" into the number 100 in the request or maybe parseFloat depending on what you need.

Then in the request body use "lat": {{lat}},