postman.getResponseCookie("csrftoken").value undefined

I was able to use these 2 lines in “Test” tab:
var xsrfCookie = postman.getResponseCookie(“csrftoken”); postman.setEnvironmentVariable(‘csrftoken’, xsrfCookie.value);
But after I relog, the code return “undefined”


Anyone know why? Thanks in advance.

Hey @huan415201

Welcome to the Postman community! :trophy:

That looks like the older sandbox syntax so we might need to change that but it also looks like the response doesn’t have any cookies so it would be undefined.

You may need to expand on the implementation details a little bit to help people understand what you’re doing.

I’m using postman to call a POST API in my backend source (Python using Django), since it requires csrftoken, I put this script in the “Tests” tab and it worked. But after I re-log my postman, I got this error, right now I have to turn off the csrf check in the backend but this is not a good way to do it, I’m not sure why this happen, is it because postman clear my cookie after I log out? if so, how can I get it generated?

So not really sure how you’re generating that cookie in your code, Postman wouldn’t be creating that for you.

That image shows that for that particular request, there were no cookies in the response. Being that there are no cookies that script is failing before there’s nothing defined at that reference.

Is this your own code or are you following some sort of tutorial - I’m curious as to where that test script syntax is coming from?

The latest app version has auto completion for the pm.* API so you would even see that older syntax in the sandbox.

I was following this article: https://hackernoon.com/automatically-set-csrf-token-in-postman-django-tips-c9ec8eb9eb5b

I’m not going to pretend to know anything about Django :grin:

From that article, it looks like it’s talking about logging into Django, maybe through some API call?

The log in mentioned has nothing to do with logging in/out of the Postman app.