Keep cookies with HTTP requests

I want to emulate loggin in to a website. Therefore I need to first do a GET on the login page, capture the cookies.
Then a second POST with the cookies to the login endpoint. After succesfull login, I do a third GET to the page I need the info from.
I don’t see possibilities in the HTTP request to store this info and pass it to the next request.
I’m using the standard version of N8N, hosted by N8N.

1 Like

Ehy @Jo_de_Boer welcome to the community!

n8n doesn’t automatically carry cookies between HTTP requests. A good workaround is to store the auth cookie manually using the Storage node.

Here’s a flow:

  1. GET login page: extract the set-cookie header.
  2. Save the cookie to a Storage node (e.g. key: “auth_cookie”).
  3. POST login: add a custom header:

Cookie: ={{ $store.get('auth_cookie') }}

  1. GET the final page using the same cookie.

I think your session is preserved between requests.

1 Like

great idea, might have to try this too :slight_smile:

Let me know! :raised_hands:

@Jo_de_Boer, Wondering if you got this working. I was puzzled by the answer you got before from @Gallo_AIA which referenced a Storage node. I couldn’t find anything in any version of n8n I’m using called that, or an expression variable named $store. Not sure if he’s referencing some community (add on) node, but I don’t think those things are built into the standard n8n.

When you call an “auth” endpoint, assuming you have the request part working, there are a few things you might need to do to actually see the set-cookie response header in the HTTP Request node output.

  1. Add the Response option
  2. Toggle on Include Response Headers and Status
  3. If the response from the auth endpoint is a redirect / 302 (which it often is)
    a. Also toggle on ‘Never Error’
    b. Also set the Response Format to Text
    c. Also add the Redirects option and toggle it off

You should then be able to access the set-cookie response from the auth request directly for the remainder of the workflow (i.e. for other requests) by using an expression that references the node. Parsing the actual cookie value out of the cookie header is a bit tricky, so I’d recommend doing it only once in a Edit Fields (Set) node.

Here’s a working example to start (it uses httpbin.org’s cookies endpoints for demo):

1 Like