Set response as variable in tests

Hello,

I want to set an item from a response as a variable.
The response I’m getting back is:

“In Queue, Id: 404d3a14-6d58-4bac-8577-d9eca8e4924a”

Is it possible to set Id as an variable?

Is your response a string?
Exactly like the one that you shared?

If yes, you can write something like this:

let s = pm.response.text(),
 id = s.slice(s.indexOf("Id: ") + 4);

pm.environment.set('id', id);

Edit:
Updated the incorrect syntax.

Considering the response is received as shown in screenshot:

Thank you for the respons!

Yes, my response is a string. If I try your code I’m getting an error:

TypeError: Cannot read property ‘slice’ of undefined.

@Coxjeffrey Can you show me a screenshot of the response that you’re receiving?

I have updated my answer above: Set response as variable in tests.

So now its working but the value I get is: a67bfa50-b1c2-4bcd-a29f-366e9f989ff8"

How do I get rid of the " at the end?

Thank you for the help so far!

You can modify the code a bit by adding -1 to the slice function as second argument.

Updated code:

let s = pm.response.text(),
 id = s.slice(s.indexOf("Id: ") + 4, -1);

pm.environment.set('id', id);
1 Like