Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

When setting headers with Azure Functions, you pass an Object with the headerName as the key and the value as the header value.

However, when creating cookies each cookie needs its own "Set-Cookie" header, but an Object key needs to be unique. How should this be handled?

e.g. The following sets two object keys as "Set-Cookie"; which my IDE is blowing up as not possible.

context.res = {
          status: 200,
          headers: {
              "Content-Type": "text/html",
              "Set-Cookie": "a=b; Secure; httpOnly; Path=/",
              "Set-Cookie": "b=c="; Secure; httpOnly; Path=/",
              "Cache-Control": "no-cache, no-store"
          body:
              '<HTML><BODY>RESPONSE</BODY></HTML>'
context.done();

UPDATE: Set multiple cookies using the "cookies" property in an HTTP response object

Unfortunately, this is currently a bug (first brought up in this issue). This isn't ideal, but here is a workaround you can use for now (note the additional white space in the "Set-Cookie "):

context.res = {
      status: 200,
      headers: {
          "Content-Type": "text/html",
          "Set-Cookie": "a=b; Secure; httpOnly; Path=/",
          "Set-Cookie ": "b=c="; Secure; httpOnly; Path=/",
          "Cache-Control": "no-cache, no-store"
      body:
          '<HTML><BODY>RESPONSE</BODY></HTML>'
context.done();
                At the bottom of that GH thread Marie gives an updated answer: there's now a context.res.cookies property which is an array of cookie objects to be sent with response.
– Max Ivanov
                Sep 27, 2020 at 22:43
                The workaround is not a solution Running a Python function in VSC ''' Microsoft.AspNetCore.Server.Kestrel.Core: Invalid non-ASCII or control character in header: 0x002 '''
– bittebak
                Jan 16, 2022 at 10:20
                This answer is definitely applicable for JavaScript/TypeScript functions. @vrdmr should be able to help with this question for Python functions! You can ask here too github.com/Azure/azure-functions-python-worker
– Marie Hoeger
                Jan 18, 2022 at 23:54

You can set multiple cookies on a response object with the property "cookies", which takes an array of Cookie objects.

To know more about Cookie objects:- Click Here

var cookieExipre = new Date();
cookieExipre.setTime(
    cookieExipre.getTime() + 2 * 60 * 1000 // Cookie exipre in 2 minutes
context.res = {
    body: "Hello",
    status: 200,
    cookies: [
            name: "cookie1",
            value: "cookie1",
            secure: true,
            httponly: true,
            path: "/",
            expires: cookieExipre,
            sameSite: "None",
            name: "cookie2",
            value: "cookie2",
            secure: isSecure,
            httponly: true,
            path: "/",
            expires: cookieExipre,
            sameSite: "None",

This also applies to Python/Django I did find the root cause for the bug https://github.com/Azure/Azure-Functions/issues/2145

The cookies are stored in a Dict. As a result, multiple http response headers are not supported. I guess the JS implementation has the same issue.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.