0

I want to save the response from two API calls but not sure how to do it in Thunder client, Postman is working fine.

var jsonDataResponse = JSON.parse(responseBody);
pm.environment.set("tin", jsonDataResponse.replace(/\s/g, ''));

One. https://generator.avris.it/api/DE/tin gives something like "21 348 504 760" and then in Postman I have this

postman image 1

But in Thunder, I have no idea how can I do that

thunder try

And I want to just the number without spaces

Two. I have this in Postman but no clue how made this in Thunder

let jsonData = JSON.parse(responseBody);
positionstring=""+jsonData.positions;

pwdstring=positionstring.replace(new RegExp(",", "g"), "");
positionstring=positionstring.replace(new RegExp(",", "g"), "\",\"");

positionstring ="\[\""+positionstring+"\"\]";
postman.setEnvironmentVariable("positions", positionstring);
postman.setEnvironmentVariable("pwd", pwdstring);

postman image 2

I tried the first one but only I get to save it to a tin variable with literally the function I want to replace(" ", "") when should be 21348504760

1 Answers1

0

Not necessary to convert JSON

The responseBody just string. So you just need to remove space from string of responseBody. And remove the Double Quote mark. It will be a string, then convert into Number()

Then assign the environment variable into tin.

const tin_string = responseBody.replace(/\s/g, '').replace(/"/g,'');

tin_number = Number(tin_string)

pm.environment.set("tin",tin_number)

If you want to debug the handling of your code in Tests tab, the console.log() is useful.

This is an example of debug.

console.log(responseBody)

const tin_string = responseBody.replace(/\s/g, '').replace(/"/g,'');
console.log(tin_string)

tin_number = Number(tin_string)
console.log(tin_number)

pm.environment.set("tin", tin_number)

enter image description here

Assigned environment variable tin

enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14
  • Thank you very much for your answer, I will improve the postman but I need is to know how to do it in thunder client. – user3717796 Aug 13 '23 at 19:06
  • You are welcome, I happy to hear you got it. Can you accept me answer? you can click up arrow it will give a 15 points even if you are new user.(less than 15 points). The thunder client question you can post new question. it will be help from other expert people. – Bench Vue Aug 13 '23 at 20:44