3

I'm working with an API set that requires a Session Key to be pulled down and used in the Authorization Header for subsequent calls to the service. The value to be added to the Header is X-CardConnect-SessionKey See below for example. The first 20-digits, everything in front of the semi-colon, needs to be included in future calls to the service once obtained.

Postman Image

This is what I'm getting -->

X-CardConnect-SessionKey →9ffcd7dc737f4b9fbdccb299b9c55f4b;expires=2017-12-28T20:54:19.652Z

This is what I need:

X-CardConnect-SessionKey →9ffcd7dc737f4b9fbdccb299b9c55f4b

In Postman I am using the following to parse the Session Key value into my environment:

var data = responseHeaders
postman.setEnvironmentVariable ("X-CardConnect-SessionKey", data["X-
CardConnect-SessionKey"])

Naturally, the full string is inserted into the Environment when I only need the first 20 digits. Is there a way to limit the character limit so that only the first 20 digits are returned?

Thanks!

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80

2 Answers2

1

You can split the value using native JS or look at using one of the Lodash _.split() functions.

var data = "9ffcd7dc737f4b9fbdccb299b9c55f4b;expires=2017-12-
28T20:54:19.652Z"

postman.setEnvironmentVariable("test", data.split(';',1)); 

Or for what you need something along these lines:

var data = responseHeaders
postman.setEnvironmentVariable("X-CardConnect-SessionKey", data.X-
CardConnect-SessionKey.split(';',1))
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
0

Was able to resolve with the following:

const data = responseHeaders;
postman.setEnvironmentVariable ("X-CardConnect-SessionKey", data["X-CardConnect-SessionKey"]); 
const sessionheader = data["X-CardConnect-SessionKey"];
postman.setEnvironmentVariable ("X-CardConnect-SessionKey",  sessionheader.substring(0, 32));
n-verbitsky
  • 552
  • 2
  • 9
  • 20
  • Cool, do you need to create all the variables? You could probably do all that in one line. Check out the `pm.response.headers()` in the [`pm.*`](https://www.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference) api that’s part of the newer versions. The syntax you’re using is in the older style and will be dropped soon. – Danny Dainton Dec 29 '17 at 17:03