How to export cookies?

cookie

#1

Is there a way to include defined cookies in a collection export? When I try to export it doesn’t include my cookies.

The export:

{
	"info": {
		"_postman_id": "77247b9a-2547-4dc8-b502-c9a6448010b0",
		"name": "Cookie",
		"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
	},
	"item": [
		{
			"name": "TestRequest",
			"request": {
				"method": "GET",
				"header": [],
				"body": {
					"mode": "raw",
					"raw": ""
				},
				"url": "example.com"
			},
			"response": []
		}
	]
}

My defined cookie, for domain example.com. It does get sent with a test request:

TestCookie=testval; path=/; domain=.example.com;


#2

I am not an authority on http cookies but I might be bale to shed some light.

First off if you are interested please read the RFC for HTTP cookie RFC6265

  • Cookies are generated and distributed by servers.
  • Cookies by definition have an expiration date.

Given the above that means that cookies are not good candidates for storing long term. If you were to store them in a collection and come back even a day later they might not be valid and would be regenerated.

What is the reason that you want to store the cookies with the requests?

If you DO want to send specific cookies with a request you would be able to set-cookie via postman in a script (See documentation)


#3

On a similar note, is there a way to pass a cookie to the next url?

In Postman, it’s done automatically; when I login, I clearly see the cookie

ASPSESSIONIDCWQCTSBR : ENCJPGFCBCDDLAFEOCPIAJDH

and when I goto the next page to do a search with x-www-form-urlencoded, it works to bring in the body of the html text, in Postman.

However, when I use PHP CURL, there is an error. I can grab the cookie from the login page:

// execute!
$response = curl_exec($ch);

// get cookie
// multi-cookie variant contributed by @Combuster in comments
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $response, $matches);
$cookies = array();
foreach($matches[1] as $item) {
    parse_str($item, $cookie);
    $cookies = array_merge($cookies, $cookie);
}
$cookieArr = [];

foreach ($cookies as $key => $value) {
	$cookieArr[$key] = $value;
}

but I can’t pass the cookie to the search page:

    $ccokie = $cookieKey.'='.$cookieValue;  //  ASPSESSIONIDCWQCTSBR = ENCJPGFCBCDDLAFEOCPIAJDH
curl_setopt($ch, CURLOPT_COOKIE, $ccokie);

It gives me a redirect in the header, like I didn’t enter the proper parameters:

reponse:HTTP/1.1 100 Continue HTTP/1.1 302 Object moved Cache-Control: no-cache Pragma: no-cache Content-Length: 130 Content-Type: text/html Expires: Sun, 10 Mar 2019 14:58:49 GMT Location: login.asp Server: Microsoft-IIS/7.0 X-Powered-By: ASP.NET Date: Sun, 10 Mar 2019 14:59:48 GMT

It is not a search issue since it would provide an improper search text in the body of the html if that were the case:

        <b>There was a problem processing your search request</b>
        <br>You must select at least one account to search.

But it just tries to redirect me to the login page again:
# Object Moved
This object may be found here.

Any ideas on how to pass cookies using CURL?

Many thanks,

G