Parse Text Output to Variable

Can someone help me with following response:

“Session Token-> BAMAuthToken: 4HyQcMTU3MjAwMzA5NTkyMzpnZXIwMDAxcF9hcGk <- for User : ger0001p_api”

I need the token stored in a variable TOKEN. (4HyQcMTU3MjAwMzA5NTkyMzpnZXIwMDAxcF9hcGk)

Thanks and best regards,
Philipp

Hey @ger0001p,

You could give this a go - It’s only going to cover a very basic way of getting and setting that value from the response:

Getting the response body as text using pm.response.text(), splitting the data using the .split() function and then creating an array of those items.

console.log(pm.response.text().split(" "))

The item is 4th in the zero-indexed list so you can reference that using [3] to save that value as an environment variable.

pm.environment.set("TOKEN", pm.response.text().split(" ")[3])

1 Like

Thank you @dannydainton. Works perfect! :slight_smile:

1 Like

You should really encourage the entity responsible for the API to provide you with a parsable response, like JSON. This is really a nasty hack which can easily break in the future.

1 Like

Agreed, This solution will not stand the test of time.