0

How do I set multiple Set-Cookie headers like google does. Tried doing it in Go and Node.js but it doesn't seem possible. Is it possible to do this without any framework(s)?

enter image description here

  • Go
res.Header().Set("Set-Cookie", "q=city,c=acc; HttpOnly; SameSite=Lax") 
  • Node.js
 res.writeHead(200, {
    "Content-Type": "text/html",
    "Set-Cookie": "q=city,c=acc; HttpOnly; SamSite=Lax", 
    "Cache-Control": "max-age=120"
  });
0xedb
  • 354
  • 2
  • 8
  • 2
    *"Tried doing it in Go and Node.js but it doesn't seem possible."* - It would be helpful if you actually documented what exactly you've tried. Duplicate of [How do I set multiple http header fields with the same key in Node.js?](https://stackoverflow.com/questions/39397983/how-do-i-set-multiple-http-header-fields-with-the-same-key-in-node-js) – Steffen Ullrich Dec 25 '20 at 23:42
  • I want to know if it's supported by the core http module in both Go/Node.js – 0xedb Dec 26 '20 at 00:58
  • 1
    @theBashShell Yes, its supported. use `setHeader` and use a array for "set-cookie"/Cookie: https://nodejs.org/api/http.html#http_request_setheader_name_value – Marc Dec 26 '20 at 00:59
  • thanks @Marc I can confirm that works in Node.js – 0xedb Dec 26 '20 at 01:08

2 Answers2

1

For golang, http.Header has a Add Method, which would append instead of overwrite existing keys.

Add adds the key, value pair to the header. It appends to any existing values associated with key. The key is case insensitive; it is canonicalized by CanonicalHeaderKey.

fefe
  • 3,342
  • 2
  • 23
  • 45
0

For Node.js, use setHeader on the response with an array as value

  res.writeHead(200, { 
    "Set-Cookie": ["q=city;HttpOnly", "lat=45; SameSite=Lax", "another=foo;SameSite=Lax;"] 
  });

For Go, use Header().Add() on the response

res.Header().Add("Set-Cookie", "q=city; SameSite=Lax")
res.Header().Add("Set-Cookie", "multiple=1; HttpOnly")
0xedb
  • 354
  • 2
  • 8