How to get Bearer and refresh value from response cookie

Hi I am new to postman and newman. I have to send cookie from request header . In order to do it i have get response header value of Set-Cookie as attached below . I just need value of the Bearer and Refresh with out Max-Age or Path/ Please help

Trying to run postman collection in Newman using linux system. I have created collection with Post login request under Folder1, stopping the ldap server on and trying to add,update,delete user requests under Folder 2 to get ā€œserver down messageā€ status 400

The issue is with the FOlder 2 . From Postman, I get a 200 OK by disabling cookie jar, and from Newman, the same request returns 401 Unauthorized not 400.
Even though i added header cookie value on Folder2 requests after stopping the server.

This is how the authorization works for this API:

2 set-cookies values are obtained from the login request, specifically from the response headers, where we have 2 Set-Cookie headers with the corresponding values for the cookies.

How Iā€™m obtaining the values and using them

In the request 1 (login) (Folder1) I have the following code in the Test tab:
// Save cookies from response headers to use it in subsequent requests
const cookies = pm.response.headers.all().filter(headerObj => headerObj.key === ā€˜Set-Cookieā€™).map(headerObj => headerObj.value.split(ā€˜;ā€™)[0]);
pm.environment.set(ā€˜cookiesā€™, cookies.join('; '));

If I print the saved values I obtain something like this:

Bearer=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJkYjZlN2QzMy04OGI4LTQ3OTQtODliYy01Y2E3ZWE0ZGJlZTkiLCJleHAiOjE2Nzk5MjI2NTcsImZyZXNoIjp0cnVlLCJpYXQiOjE2Nzk5MjE3NTcsInR5cGUiOiJhY2Nlc3MiLCJuYmYiOjE2Nzk5MjE3NTcsImlkZW50aXR5Ijp7InVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoic3VwZXJ1c2VycyIsInJqdGkiOiI3YWY3ZmZjZi0yOGI1LTQ0YjQtOTFhYS1hMGEyODliYzRlYjMifX0.bJ5db8W5orFIWqTBUygZA8mK2y6AjFs7N8dvax_LZVY; Refresh=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3YWY3ZmZjZi0yOGI1LTQ0YjQtOTFhYS1hMGEyODliYzRlYjMiLCJleHAiOjE2ODI1MTM3NTcsImlhdCI6MTY3OTkyMTc1NywidHlwZSI6InJlZnJlc2giLCJuYmYiOjE2Nzk5MjE3NTcsImlkZW50aXR5Ijp7InVzZXJuYW1lIjoiYWRtaW4iLCJzZXNzaW9uVGltZW91dCI6MTUsInJvbGUiOiJzdXBlcnVzZXJzIn19.S5f0A6HC9XNe4xMo6ODKLzYyXLOoUGsDqz8sguH-10o

In the request 2 (Folder2) (Edit users) I do the following in the pre-request tab:

pm.request.headers.add({
key: ā€œCookieā€,
value: pm.environment.get(ā€œcookiesā€)
});
Note: Also tried inserting the varaibles directly in headers ex: Cookie: {{cookies}} but with the same result.

So, if I run this from Postman, this second request respond 200 and the item is created (expected response).
When running it from Newman I get 401. Please help

Here is the new man shell script which i am running , both the folders are under ā€œldap-down.postman_collection.jsonā€
newman run ldap-down.postman_collection.json -e L1-70.postman_environment.json --folder folder1 -r htmlextra -k
systemctl stop slapd
newman run ldap-down.postman_collection.json -e L1-70.postman_environment.json --folder folder2 -r htmlextra -k
systemctl start slapd

Trying to run postman collection in Newman using linux system. I have created collection with Post login request under Folder1, stopping the ldap server on and trying to add,update,delete user requests under Folder 2 to get ā€œserver down messageā€ status 400

The issue is with the FOlder 2 . From Postman, I get a 200 OK by disabling cookie jar, and from Newman, the same request returns 401 Unauthorized not 400.
Even though i added header cookie value on Folder2 requests after stopping the server.

This is how the authorization works for this API:

2 set-cookies values are obtained from the login request, specifically from the response headers, where we have 2 Set-Cookie headers with the corresponding values for the cookies.

How Iā€™m obtaining the values and using them

In the request 1 (login) (Folder1) I have the following code in the Test tab:
// Save cookies from response headers to use it in subsequent requests
const cookies = pm.response.headers.all().filter(headerObj => headerObj.key === ā€˜Set-Cookieā€™).map(headerObj => headerObj.value.split(ā€˜;ā€™)[0]);
pm.environment.set(ā€˜cookiesā€™, cookies.join('; '));

If I print the saved values I obtain something like this:

Bearer=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJkYjZlN2QzMy04OGI4LTQ3OTQtODliYy01Y2E3ZWE0ZGJlZTkiLCJleHAiOjE2Nzk5MjI2NTcsImZyZXNoIjp0cnVlLCJpYXQiOjE2Nzk5MjE3NTcsInR5cGUiOiJhY2Nlc3MiLCJuYmYiOjE2Nzk5MjE3NTcsImlkZW50aXR5Ijp7InVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoic3VwZXJ1c2VycyIsInJqdGkiOiI3YWY3ZmZjZi0yOGI1LTQ0YjQtOTFhYS1hMGEyODliYzRlYjMifX0.bJ5db8W5orFIWqTBUygZA8mK2y6AjFs7N8dvax_LZVY; Refresh=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3YWY3ZmZjZi0yOGI1LTQ0YjQtOTFhYS1hMGEyODliYzRlYjMiLCJleHAiOjE2ODI1MTM3NTcsImlhdCI6MTY3OTkyMTc1NywidHlwZSI6InJlZnJlc2giLCJuYmYiOjE2Nzk5MjE3NTcsImlkZW50aXR5Ijp7InVzZXJuYW1lIjoiYWRtaW4iLCJzZXNzaW9uVGltZW91dCI6MTUsInJvbGUiOiJzdXBlcnVzZXJzIn19.S5f0A6HC9XNe4xMo6ODKLzYyXLOoUGsDqz8sguH-10o

In the request 2 (Folder2) (Edit users) I do the following in the pre-request tab:

pm.request.headers.add({
key: ā€œCookieā€,
value: pm.environment.get(ā€œcookiesā€)
});
Note: Also tried inserting the varaibles directly in headers ex: Cookie: {{cookies}} but with the same result.

So, if I run this from Postman, this second request respond 200 and the item is created (expected response).
When running it from Newman I get 401. Please help

Here is the new man shell script which i am running , both the folders are under ā€œldap-down.postman_collection.jsonā€
newman run ldap-down.postman_collection.json -e L1-70.postman_environment.json --folder folder1 -r htmlextra -k
systemctl stop slapd
newman run ldap-down.postman_collection.json -e L1-70.postman_environment.json --folder folder2 -r htmlextra -k
systemctl start slapd
Please help

I canā€™t test this or produce example code as I canā€™t mirror the request\response.

The logic will be something similar to the following.

  • Retrieve ā€œSet-Cookieā€ from header
  • const responseSetCookie = pm.response.headers.get(ā€˜Set-Cookieā€™);
  • As you have two entries, Iā€™m guessing this will produce an array. (Not sure as I canā€™t replicate this)
  • You will then need to search the array for the entry that begins ā€˜bearerā€™ and another search for ā€˜refreshā€™.
  • You should be able to use the JavaScript find function to do this.
  • JavaScript Array find() Method (w3schools.com)
  • Then parse the cookie for each of the searches

The following website has an example on how to parse a cookie to get the various elements.

Postman uses JavaScript under the hood, so when you have these types of queries, you can google for JavaScript examples.

Thank you , I got it yesterday and replied to it but it got temporarily hidden my posts .
But now i have issue to pass cookie in the request header when i run collection using newman
Trying to run postman collection in Newman using linux system. I have created collection with Post login request under Folder1, stopping the ldap server on and trying to add,update,delete user requests under Folder 2 to get ā€œserver down messageā€ status 400

The issue is with the FOlder 2 . From Postman, I get a 200 OK by disabling cookie jar, and from Newman, the same request returns 401 Unauthorized not 400.
Even though i added header cookie value on Folder2 requests after stopping the server.

This is how the authorization works for this API:

2 set-cookies values are obtained from the login request, specifically from the response headers, where we have 2 Set-Cookie headers with the corresponding values for the cookies.

How Iā€™m obtaining the values and using them

In the request 1 (login) (Folder1) I have the following code in the Test tab:
// Save cookies from response headers to use it in subsequent requests
const cookies = pm.response.headers.all().filter(headerObj => headerObj.key === ā€˜Set-Cookieā€™).map(headerObj => headerObj.value.split(ā€˜;ā€™)[0]);
pm.environment.set(ā€˜cookiesā€™, cookies.join('; '));

If I print the saved values I obtain something like this:

Bearer=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJkYjZlN2QzMy04OGI4LTQ3OTQtODliYy01Y2E3ZWE0ZGJlZTkiLCJleHAiOjE2Nzk5MjI2NTcsImZyZXNoIjp0cnVlLCJpYXQiOjE2Nzk5MjE3NTcsInR5cGUiOiJhY2Nlc3MiLCJuYmYiOjE2Nzk5MjE3NTcsImlkZW50aXR5Ijp7InVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoic3VwZXJ1c2VycyIsInJqdGkiOiI3YWY3ZmZjZi0yOGI1LTQ0YjQtOTFhYS1hMGEyODliYzRlYjMifX0.bJ5db8W5orFIWqTBUygZA8mK2y6AjFs7N8dvax_LZVY; Refresh=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI3YWY3ZmZjZi0yOGI1LTQ0YjQtOTFhYS1hMGEyODliYzRlYjMiLCJleHAiOjE2ODI1MTM3NTcsImlhdCI6MTY3OTkyMTc1NywidHlwZSI6InJlZnJlc2giLCJuYmYiOjE2Nzk5MjE3NTcsImlkZW50aXR5Ijp7InVzZXJuYW1lIjoiYWRtaW4iLCJzZXNzaW9uVGltZW91dCI6MTUsInJvbGUiOiJzdXBlcnVzZXJzIn19.S5f0A6HC9XNe4xMo6ODKLzYyXLOoUGsDqz8sguH-10o

In the request 2 (Folder2) (Edit users) I do the following in the pre-request tab:

pm.request.headers.add({
key: ā€œCookieā€,
value: pm.environment.get(ā€œcookiesā€)
});
Note: Also tried inserting the varaibles directly in headers ex: Cookie: {{cookies}} but with the same result.

So, if I run this from Postman, this second request respond 200 and the item is created (expected response).
When running it from Newman I get 401. Please help

Here is the new man shell script which i am running , both the folders are under ā€œldap-down.postman_collection.jsonā€
newman run ldap-down.postman_collection.json -e L1-70.postman_environment.json --folder folder1 -r htmlextra -k
systemctl stop slapd
newman run ldap-down.postman_collection.json -e L1-70.postman_environment.json --folder folder2 -r htmlextra -k
systemctl start slapd

You are running the Newman command line twice in your script.

It sounds like you are setting the cookie when you call Newman the first time.

When you call Newman the second time, you are using the same environment file you called the first request with. It will not contain the Cookie header that you set the first time you called Newman. It will not persist between the requests.

If you want the values to persists between the requests, then they need to be run from the same Newman call by having both folders in a collection and running the entire collection. The value will then persist.