0
votes

having problems with quotes in postman. I've set the request body as json: {{request_body}}

This is the code in the pre-request:

var body = {"data":{"Version":"{{foo}}"}} 
pm.environment.set("foo",pm.globals.get("x")); 
var body_str = JSON.stringify(body);
 pm.environment.set('request_body', body_str);

The request body is:

{"data":{"Version":"125"}}

But I want it to be without quotes:

{"data":{"Version":125}}

If I try to delete the quotes from the pre-request it gives me error:

There was an error in evaluating the Pre-request Script: SyntaxError: Unexpected token {

1

1 Answers

0
votes

first remove double quotes in "{{foo}}" and try if not works that's mean foo saved as string "123" so you have to cast it to number by the below ,

Get foo variable and cast it to number then add it to body without quotes , finally stringify the body.

var foo = Number(pm.globals.get("x"));

var body = {"data":{"Version": foo }} ;

pm.environment.set("foo",pm.globals.get("x")); 

var body_str = JSON.stringify(body);

pm.environment.set('request_body', body_str);

Please check how stringify works :

  var body = {"data":{"Version":"123"}} 
  JSON.stringify(body) => '{"data":{"Version":"123"}}'


  var body = {"data":{"Version":123}} 
  JSON.stringify(body) => '{"data":{"Version":123}}'