Set Variable (env or global) from a url string

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

:slight_smile:

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

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

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

Hopefully, this helps!

Sorry about the inexperience :confused:

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= 890890890809890809890 &state=xyz"

from my response?

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. :slight_smile:

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

exactly like that :slight_smile:

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?

trying to pull

code= “xxxxxxxxxxxxxx

just plain string

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?

The whole string was set :slight_smile:

Just need to pull the code out of the string now

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])

KING!!!
Thank you again Danny

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

https://script.google.com/macros/d/1WsrBafTT3Ofqp2Znw_kXDyZUNd/usercallback?code= 890890890809890809890 &state=xyz"

How can I do? please help me

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