Search code examples
httpsetcookie

How to write multi cookies from server side to browser?


I'm using vert.x to write an application. It doesn't have built-in cookie support yet, and we have to use "putHeader()" method to manually set cookies.

Now I want to set several cookies, so I write:

 req.response.putHeader("Set-Cookie", "aaa=111; path=/")
 req.response.putHeader("Set-Cookie", "bbb=222; path=/")
 req.response.putHeader("Set-Cookie", "ccc=333; path=/")

But I found vert.x send only one "Set-Cookie":

 Set-Cookie ccc=333; path=/

I'm not sure if I misunderstand something. Can server send multi "Set-Cookie" commands one time? Is it correct to send multi cookies this way?


Solution

  • I think no, it's impossible out of the box because headers stored in a HashMap: https://github.com/purplefox/vert.x/blob/master/src/main/java/org/vertx/java/core/http/impl/DefaultHttpServerResponse.java#L81

    You can:

    • Open new issue
    • Comment existing issue https://github.com/purplefox/vert.x/issues/89
    • Checkout source and use map what allow duplicate keys Map implementation with duplicate keys (you need handle duplicate manually, for instance Location-header should be only one time
    • Extend DefaultHttpServerResponse and see how you can integrate it
    • Merge cookies and handle it manually, for instance:

      req.response.putHeader("Set-Cookie", "aaa=111&bbb=222&ccc=333; path=/")