# Set Variable (env or global) from a url string

**URL:** <https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751>\
**Category:** Help Hub\
**Created:** [April 6, 2020, 6:30pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751 "2020-04-06T18:30:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainwave22](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rainwave22/32/3833_2.png) [@rainwave22](https://community.postman.com/u/rainwave22)\
**Post date:** [April 6, 2020, 6:30pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/1 "2020-04-06T18:30:37Z")

</div>

Good Afternoon all  
Hope you are all well

I am trying to set a variable form extracting the **CODE** (in bold below) this string response

https://script.google.com/macros/d/1WsrBafTT3Ofqp2Znw\_kXDyZUNd/usercallback?code= **890890890809890809890** &state=xyz"

Any ideas would be super helpful

🙂

---

<div class="post-metadata">

**Author:** ![SabriH](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@SabriH](https://community.postman.com/u/SabriH)\
**Post date:** [April 6, 2020, 9:44pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/2 "2020-04-06T21:44:18Z")

</div>

Hey @rainwave22,

I’ve uploaded a template to help provide some guidance on the way to achieve that. It can be found here: [https://documenter.getpostman.com/view/10582367/SzYdSbmk?version=latest](https://documenter.getpostman.com/view/10582367/SzYdSbmk?version=latest)

You’ll want to look at the tests tab to see it. It would involve making use of the `url` and `querystring` libraries to fetch the information. More information available here: [https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#require](https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#require)

You could then set the environment variable or global by using the likes of `pm.environment.set` if needed. More information about that here: [https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#require](https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#require)

Hopefully, this helps!

---

<div class="post-metadata">

**Author:** ![rainwave22](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rainwave22/32/3833_2.png) [@rainwave22](https://community.postman.com/u/rainwave22)\
**Post date:** [April 7, 2020, 3:31pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/3 "2020-04-07T15:31:04Z")

</div>

Sorry about the inexperience 😕

but I am trying to do

```
let Data = pm.response.json();
pm.collectionVariables.set("code", pm.response.json().code[0].code); 

```

to get  
890890890809890809890

[https://script.google.com/macros/d/1WsrBafTT3Ofqp2Znw\_kXDyZUNd/usercallback?code=](https://script.google.com/macros/d/1WsrBafTT3Ofqp2Znw_kXDyZUNd/usercallback?code=) **890890890809890809890** &state=xyz"

from my response?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 7, 2020, 4:20pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/4 "2020-04-07T16:20:56Z")

</div>

Hey @rainwave22,

What does your `Response Body` look like?

If we can get that exactly how it’s received, it will be easier to offer a more targeted solution. 🙂

---

<div class="post-metadata">

**Author:** ![rainwave22](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rainwave22/32/3833_2.png) [@rainwave22](https://community.postman.com/u/rainwave22)\
**Post date:** [April 7, 2020, 4:29pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/5 "2020-04-07T16:29:18Z")

</div>

[https://script.google.com/macros/d/xxxxxxxxxxxxxxxxxxxxxxxx/usercallback?code=xxxxxxxxxxxxxx&state=xyz](https://script.google.com/macros/d/xxxxxxxxxxxxxxxxxxxxxxxx/usercallback?code=xxxxxxxxxxxxxx&state=xyz)

---

<div class="post-metadata">

**Author:** ![rainwave22](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rainwave22/32/3833_2.png) [@rainwave22](https://community.postman.com/u/rainwave22)\
**Post date:** [April 7, 2020, 4:29pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/6 "2020-04-07T16:29:36Z")

</div>

exactly like that 🙂

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 7, 2020, 4:33pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/7 "2020-04-07T16:33:06Z")

</div>

Just as a plain text string?

So if you were to do something like:

`pm.globals.set("url", pm.response.text())`

It would set that URL as the global variable?

---

<div class="post-metadata">

**Author:** ![rainwave22](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rainwave22/32/3833_2.png) [@rainwave22](https://community.postman.com/u/rainwave22)\
**Post date:** [April 7, 2020, 4:34pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/8 "2020-04-07T16:34:38Z")

</div>

trying to pull

code= “ **xxxxxxxxxxxxxx** ”

just plain string

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 7, 2020, 4:37pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/9 "2020-04-07T16:37:01Z")

</div>

I know what you’re trying to do but what the response is and the format it’s returned in is very important.

So does the code I posted, set the **whole** string, as a global variable when run?

---

<div class="post-metadata">

**Author:** ![rainwave22](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rainwave22/32/3833_2.png) [@rainwave22](https://community.postman.com/u/rainwave22)\
**Post date:** [April 7, 2020, 4:42pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/10 "2020-04-07T16:42:28Z")

</div>

The whole string was set 🙂

Just need to pull the code out of the string now

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 7, 2020, 4:43pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/11 "2020-04-07T16:43:50Z")

</div>

This is completely hacky and horrible but if you just want the code value, it might do:

```
let str = pm.response.text()

pm.globals.set("code", str.split("=")[1].split("&")[0])
```

---

<div class="post-metadata">

**Author:** ![rainwave22](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rainwave22/32/3833_2.png) [@rainwave22](https://community.postman.com/u/rainwave22)\
**Post date:** [April 7, 2020, 4:44pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/12 "2020-04-07T16:44:55Z")

</div>

KING!!!  
Thank you again Danny

---

<div class="post-metadata">

**Author:** ![cryosat-physicist-41](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@cryosat-physicist-41](https://community.postman.com/u/cryosat-physicist-41)\
**Post date:** [June 18, 2021, 3:48am UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/13 "2021-06-18T03:48:47Z")

</div>

About trying to set a variable form extracting the **state** (in bold below)

[https://script.google.com/macros/d/1WsrBafTT3Ofqp2Znw\_kXDyZUNd/usercallback?code=](https://script.google.com/macros/d/1WsrBafTT3Ofqp2Znw_kXDyZUNd/usercallback?code=) 890890890809890809890 & **state=xyz"**

How can I do? please help me

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 23, 2023, 3:47pm UTC](https://community.postman.com/t/set-variable-env-or-global-from-a-url-string/11751/14 "2023-08-23T15:47:25Z")

</div>

Closed on August 23, 2023, 3:47pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
