# Set response as variable in tests

**URL:** <https://community.postman.com/t/set-response-as-variable-in-tests/7449>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [August 27, 2019, 9:39am UTC](https://community.postman.com/t/set-response-as-variable-in-tests/7449 "2019-08-27T09:39:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Coxjeffrey](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Coxjeffrey](https://community.postman.com/u/Coxjeffrey)\
**Post date:** [August 27, 2019, 9:39am UTC](https://community.postman.com/t/set-response-as-variable-in-tests/7449/1 "2019-08-27T09:39:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [August 27, 2019, 11:43am UTC](https://community.postman.com/t/set-response-as-variable-in-tests/7449/2 "2019-08-27T11:43:18Z")

</div>

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

If yes, you can write something like this:

```javascript
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:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/c/0ce17d484c8fa980eedc24bf6ce258d61f984024.png)

---

<div class="post-metadata">

**Author:** ![Coxjeffrey](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Coxjeffrey](https://community.postman.com/u/Coxjeffrey)\
**Post date:** [August 29, 2019, 8:00am UTC](https://community.postman.com/t/set-response-as-variable-in-tests/7449/3 "2019-08-29T08:00:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [August 29, 2019, 8:01am UTC](https://community.postman.com/t/set-response-as-variable-in-tests/7449/4 "2019-08-29T08:01:47Z")

</div>

@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](https://community.postman.com/t/set-response-as-variable-in-tests/7449/2).

---

<div class="post-metadata">

**Author:** ![Coxjeffrey](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Coxjeffrey](https://community.postman.com/u/Coxjeffrey)\
**Post date:** [September 3, 2019, 7:19am UTC](https://community.postman.com/t/set-response-as-variable-in-tests/7449/5 "2019-09-03T07:19:35Z")

</div>

![postman](https://us1.discourse-cdn.com/getpostman/original/3X/1/7/1780a72d14e65c935c7de1ef1f97f5cea4537e3b.png)

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!

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [September 8, 2019, 7:25am UTC](https://community.postman.com/t/set-response-as-variable-in-tests/7449/6 "2019-09-08T07:25:48Z")

</div>

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

Updated code:

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

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

```
