31

I need to get the value of a cookie, that is only created when I send a request in postman.

I have tried everything but I don't know how to achieve this. I also need to store that value to use in further requests for my project.

cookie name: JSESSIONID value:09840****************00C44

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
Azzok
  • 423
  • 1
  • 4
  • 12
  • 4
    Please ask a clear question. I don't get anything from that long sentence missing all punctuation – Nico Haase Mar 13 '18 at 15:01
  • The question is actually vague and lacking details but I can provide an answer to this as it’s something i’ve answered before - I bit more searching would have made this question redundant. – Danny Dainton Mar 13 '18 at 22:48
  • Try this:- var value = postman.getResponseCookie("key").value; – vsk.rahul Mar 06 '21 at 22:21

1 Answers1

64

You could use the pm.environment.set('my_cookie', pm.cookies.get('JSESSIONID')) function in the Tests tab and store it as an environment variable.

This can then be used in the next Request Body (If chaining them together) or in any Request Body or Request Header by referencing the value using the {{my_cookie}} syntax.

A very similar issue can be found here.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • A precision: to be saved and re-used, the env variable must have been declared in a managed environment in Postman (the user should first create an environment if he's not using one, then declare an environment variable, and then he can set its value using the aforementioned command in Tests). Plus, in recent versions of Postman I think the syntax has changed, it is now: `postman.setEnvironmentVariable('my_cookie', pm.cookies.get('JSESSIONID'));` – Cécile Fecherolle Mar 05 '20 at 16:27
  • It's the opposite way around, that would be the older syntax you have used. To overcome the need to create an environment beforehand,you could swap out `environment` and use either `collectionVariables` or `globals` in it's place. These wouldn't need to be created before the request is sent. – Danny Dainton Mar 05 '20 at 18:12
  • My bad, I was indeed using the older syntax without knowing. Thanks for the complementary info! – Cécile Fecherolle Mar 06 '20 at 09:00
  • Can you explain what "next/any" is and where to find this option in postman? – cglacet Feb 21 '23 at 10:10
  • By that, I think I meant (It was a while ago) `next` request in the Collection, if you're chaining the requests and running them sequentially or `any` request body if the value is set in an accessible variable scope. – Danny Dainton Feb 21 '23 at 10:41