# Reusing set-cookie from response headers in HTTP request

**URL:** <https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482>\
**Category:** General\
**Created:** [8 September 2022 13:52 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482 "2022-09-08T13:52:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 13:52 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/1 "2022-09-08T13:52:39Z")

</div>

Hi all,

I'm struggling a bit to create an http flow in node-red... I've been fighting for some days, going ahead, but not achieving the final result, so any help will be a lot 🙂

This is tied to [this issue](https://discourse.nodered.org/t/http-request-cookies/58889), but now closed.

I'm trying to fetch info from a web server which login is controlled by a cookie parameter set as http header, as usual.

When loading the login page the server sends a "set-cokie" parameter in the response headers. I'd need to reuse that parameter which comes in this form:

```auto
set-cookie: ASPSESSIONIDCUSRARBR=OLJCEELALEKFHOJOCEEEKAHH; secure; path=/

```

I'd need to reuse that cookie value (removing the last part I guess) to make requests adding in headers of the http request in this format (this is the example from curl command):

```auto
  -H 'cookie: ASPSESSIONIDCUSRARBR=OLJCEELALEKFHOJOCEEEKAHH'

```

I tried several ways, some cleaner than others... but non worked 🤣

Some of the ideas... first one was to try to use it directly:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81a1ecd8176b057a1f60f279c0421c689ce44909.png)

Other idea has been (as I usually inject headers in HTPP requests in node-red) with a change node... and for this i was trying to change msg.headers with `"'cookie: " & $msg.headers["set-cookie"][0] & "'"`... but it doesnt come with the cookie value in the middle of the sentence...

Last idea Im trying is with a function...

```auto
msg.headers = msg.headers || {};
msg.headers["cookie"] = msg.headers["set-cookie"]
delete msg.payload

let avcookie = ""
let newMsg = {} // clean new msg

// filter cookies from previous reply 
msg.headers["cookie"].forEach(el => {

    if (el.startsWith("ASPSESSIONID")) avcookie = el.split(";")[0]
})

newMsg.headers = {
    "Cookie": `${avcookie}`,
}
//delete msg.headers["set-cookie"]
return newMsg;

```

And then with a change nod using newMsg as msg.headers...

But non of them worked, all the ideas come from other msgs and forums... thats why some are better/cleaner than others.

I'd like to use the more simple option that exists... if it can be without a function and reusing that set-cookie value somehow... that would be the best 🙂

I'm suspecting that my issue is there in the cookie thing... but there is other part of the login request sent in the initial POST (after getting the set-cookie in a previous GET), when doing all of this manually is like:

```auto
  -H 'cookie: ASPSESSIONIDCUSRARBR=OLJCEELALEKFHOJOCEEEKAHH \
  --data-raw 'nic=EMAIL&pwd=PASSWORD&Enviar=Entrar+%2F+Enter'

```

This part im adding it as msg.payload with a change node injected in login page. As an string, without the simple quotes. Maybe all the headers cookie was right in some if the tries and the issue was here now that im thinking xD, I hope not!

Thanks!

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 14:10 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/2 "2022-09-08T14:10:57Z")

</div>

The full flow in curl commands, just if anyone needs to know how this is done from a manual form:

First to get the cookie I do a normal GET, and get it in responde headers (it changes on every request as far as I see):

```auto
< HTTP/2 200
< cache-control: Private
< pragma: no-cache
< content-type: text/html
< expires: Wed, 05 Jan 2000 11:12:12 GMT
< server: Microsoft-IIS/10.0
< set-cookie: ASPSESSIONIDCUSRARBR=OLJCEELALEKFHOJOCEEEKAHH; secure; path=/
< x-powered-by: ASP.NET
< date: Thu, 08 Sep 2022 13:40:45 GMT

```

Then to login:

```auto
curl 'https://URL/default.asp' \
  -H 'cookie: ASPSESSIONIDCUSRARBR=OLJCEELALEKFHOJOCEEEKAHH' \
  --data-raw 'nic=EMAIL&pwd=PASSWORD&Enviar=Entrar+%2F+Enter'

```

Then some middle webpage that loads nothing but its needed as far as I've tested:

```auto
curl 'https://URL/cargaDatos.asp' \
  -H 'cookie: ASPSESSIONIDCUSRARBR=OLJCEELALEKFHOJOCEEEKAHH

```

And finally the webpage that give the HTML content where im parsing some stuff:

```auto
curl 'https://URL/inicio.asp' \
  -H 'cookie: ASPSESSIONIDCUSRARBR=OLJCEELALEKFHOJOCEEEKAHH'

```

When doing the 3 GETs... one after the other manually in this order (as seen in Chrome inspect), It works,

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 September 2022 16:58 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/3 "2022-09-08T16:58:26Z")

</div>

If you check the request node documentation:

> Cookie handling

> The `cookies` property passed to the node must be an object of name/value pairs. The value can be either a string to set the value of the cookie or it can be an object with a single `value` property.

> Any cookies returned by the request are passed back under the `responseCookies` property.

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 17:21 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/4 "2022-09-08T17:21:14Z")

</div>

Thanks for your reply, yes that was other way I was playing with, so many ideas and i forgot some for sure in the initial post:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4badf9cae61b659ac8977965a7bcce44a9a19038.png)  
But it provides very similar content as in `msg.headers["set-cookie"]` so I continued with the other, the thing is that I don't know how to reuse it in a Request after fetching the cookie in either way.

If I use it as headers for the next request like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/639af9fd4af7f96bac5d5d04f152c381d0e19dd8.png)  
It doesnt work... maybe the unnamed object (first screenshot of this post) should be renamed as "cookie" as in the curls in my second post? or I need more coffee?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 September 2022 17:30 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/5 "2022-09-08T17:30:30Z")

</div>

Could you try:

```auto
msg.headers = {cookie: msg.responseCookies.ASPSESSIONIDCUSRARBR.value}

```

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 17:44 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/6 "2022-09-08T17:44:23Z")

</div>

Do you mean here? It gives a node-red error, it doesnt seems the format that field expects  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/418d207a4d18545a585bdb30f07d4c035fc30104.png)  
I guess you mean in other place, right?

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 17:47 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/7 "2022-09-08T17:47:31Z")

</div>

Or this? but same error, about json format maybe, something i can investigate... but is this the place you mean?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a8d40a3721afd414cb18c6233ef8d293e9412b7c.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 September 2022 17:48 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/8 "2022-09-08T17:48:30Z")

</div>

No either in a function node or set the change node to `J` as in jsonata and set the value to:

```auto
{"cookie": responseCookies.ASPSESSIONIDCUSRARBR.value}

```

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 17:48 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/9 "2022-09-08T17:48:47Z")

</div>

btw, from this image:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4badf9cae61b659ac8977965a7bcce44a9a19038.png)  
Both values change on every request, so we should reuse the ASPSESSIONIDXXXX and the following value also, both change.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 September 2022 17:50 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/10 "2022-09-08T17:50:33Z")

</div>

Yes I was wondering if that was the case (as it should change with each new session) but your examples all showed the same.

Then use jsonata in the change node and set the value to:

```auto
{"cookie": **.value}

```

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 17:51 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/11 "2022-09-08T17:51:32Z")

</div>

better yeah, but we'd need to maintain both variables, not just the value, also the variable name:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/63078d4c492e372315ab77300cae7ccb83575bd8.png)  
Because the cookie value must be both fields, like in the examples of the first posts.

Thanks so much btw, this gives me area to investigate further 🙂

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 17:53 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/12 "2022-09-08T17:53:50Z")

</div>

Same, only one value (seems the second, not the ASPSESSIONIDXXXX):  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/f/1fc61110adaa54de5d367f7141f24ca31ff472ea.png)

Update, the difference between before and after change node:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/951d8a47ce4d2364194086885f245b96935b381c.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 September 2022 17:56 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/13 "2022-09-08T17:56:36Z")

</div>

Try it like this:

```auto
{
    "cookie":*.$keys()&"="&**.value
}

```

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 17:58 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/14 "2022-09-08T17:58:22Z")

</div>

This last one im still trying to understand it 😆 , but doesnt work:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05fbdf8b9731742ade787c26f8635869044aa6a4.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 September 2022 18:00 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/15 "2022-09-08T18:00:32Z")

</div>

Sorry i forgot about the rest of the object

```auto
{
    "cookie":responseCookies.$keys()[0]&"="&**.value
}

```

edit  
This is more error proof:

```auto
{
    "cookie":responseCookies.$keys()[0]&"="&responseCookies.*.value
}

```

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 18:06 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/16 "2022-09-08T18:06:54Z")

</div>

> [@bakman2](#):
>
> ```auto
> {
> "cookie":*.$keys()&"="&**.value
> }
> 
> ```

With that I get this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7cbb718aad753fa31b5d7a17302800a00687aef7.png)  
Not sure if is same format as in the curls from my first post :S, doesnt work anyway.

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [8 September 2022 18:07 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/17 "2022-09-08T18:07:28Z")

</div>

And now I have tried this: (trying to invent... but gave no error at least for creating the parameter hehehe)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/5477d47ed3838ed0c765ba68f5dffa2afeb0f19a.png)

And I see the cookie parameter created in the headers (among rest of responseHeaders stuff)... but I get other error, related to this or maybe the raw-data with the login info?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9bc97e84d4a02b1ebb0d2d90577d016309b02a84.jpeg)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 September 2022 18:12 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/18 "2022-09-08T18:12:36Z")

</div>

> Not sure if is same format as in the curls from my first post :S, doesnt work anyway.

it is the same format.

the error comes from a different node (default.asp) ?

---

<div class="post-metadata">

**Author:** ![josechu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josechu/32/67504_2.png) [@josechu](https://discourse.nodered.org/u/josechu)\
**Post date:** [9 September 2022 15:26 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/19 "2022-09-09T15:26:20Z")

</div>

Maybe all this is from a different thing... oh my... Could be the raw data injected for the login?  
In my manual curl is something like:

```auto
curl 'https://URL/default.asp' \
  -H 'cookie: ASPSESSIONIDCUSRARBR=OLJCEELALEKFHOJOCEEEKAHH' \
  --data-raw 'nic=EMAIL&pwd=PASSWORD&Enviar=Entrar+%2F+Enter'

```

So they way I inject it in the HTTP request node is this way:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/95f66b6d0532667c086b91461fd5035320773c95.png)  
I have tried also without quotes... or in JSON format like `{"nic":"EMAIL","pwd":"PASS","Enviar":"Entrar+%2F+Enter"}`  
maybe other format is needed?

BTW, sorry I didnt reply earlier yesterday... but faced this thing hehe  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b29d08bd49eb6f1c7ff1f09cbc75789bf536043d.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 September 2022 20:37 UTC](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482/20 "2022-09-09T20:37:48Z")

</div>

data-raw is a POST request, needs to be an object.

[Next page](https://discourse.nodered.org/t/reusing-set-cookie-from-response-headers-in-http-request/67482.md?page=2)
