How to capture field value from text response

Hi, I need to capture the value of a token from a text response. I am able to capture the whole response using “var responseText = pm.response.text();”. In that i need to capture the access_token value. Can someone please guide me as to how i can get the access_token value?

Response:

**access_token**=XXX&expires_in=600&token_type=Bearer&scope=trapi.data.wealth-clients.advisor_dashboard&refresh_token=79a8d6db-4052-4301-88f3-95ce4eb0521e

Hey @ramofficial1 :wave:

You use something like this:

let str = pm.response.text(),
    accessToken = str.split('&').find(param => param.startsWith('access_token')).split('=')[1];

console.log(accessToken); 

There are a few other ways that you could do it and i’m sure others will respond on the topic telling you another way but if you wanted something quick, that should do.

1 Like

Thanks Danny. I am able to extract the access token using the code provided.

@danny-dainton

Do you know why we can’t use URLSearchParams?

The Sandbox documentation says that the url nodeJS library is available to use in the Sandbox, so this method should be available??

https://learning.postman.com/docs/tests-and-scripts/write-scripts/postman-sandbox-api-reference/#using-external-libraries

You get a reference error if you try to use it.

image

Is the library not fully implemented?

Is that relevant to the topic here? Did you need that to provide a solution?

I don’t know what is and isn’t exactly part of those external libraries - Some of them are not the full library due to various reasons related to the sandbox environment.

Yes, it would have allowed you to parse the query parameters in the string.

Never mind if its not supported in the sandbox.

1 Like

As I mentioned, there would be many different ways to achieve the same outcome. :grin:

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