I'm trying to make this uuid being set as an environmental variable after it is returned by the method. However, in the response, the method returns String only (uuid), without any variable key. I'm a total noob and tried something like:
var jsonData = pm.response.json();
pm.environment.set(String, jsonData.invitationId);
I have no idea what should I provide in the place of variable_key. Whatever I do, in the Test Results console I get errors like:
Unexpected token 'd' at 1:1 d7723dd8-31c5-40fc-a3a4-5c8a497d2280 ^
CodePudding user response:
The screenshot shows that the response is 'text' format not json
You want to save the response as text:
var res = pm.response.text();
pm.environment.set("UUID_1", res)
If you have multiple UUIDs in a response like:
c7428a7c-48b5-4636-a4c4-2c4eee71cd57
b7428a7c-58b5-3636-b4s4-1c4hgd71ad45
a7428a7c-68b5-2636-c4e4-ac4lts71ef89
then you can split them like this
var res = pm.response.text();
pm.environment.set("UUID_1", res.split("\r\n")[0])
pm.environment.set("UUID_2", res.split("\r\n")[1])
pm.environment.set("UUID_3", res.split("\r\n")[2])
CodePudding user response:
The String in the 2nd line is the error i guess. It should be a variable like below:
var url = pm.request.url;
var id = /[^\/]*$/.exec(url)[0];
pm.environment.set('testId', id);