0

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?

Freewind
  • 193,756
  • 157
  • 432
  • 708

3 Answers3

2

Use netty's io.netty.handler.codec.http.ServerCookieEncoder functionality:

req.response.putHeader("Set-Cookie", 
        ServerCookieEncoder.encode(new DefaultCookie("aaa", "111")))

there're many useful method signatures:

ServerCookieEncoder.encode(Cookie cookie)
ServerCookieEncoder.encode(Cookie... cookies)
ServerCookieEncoder.encode(Collection<Cookie> cookies)
ServerCookieEncoder.encode(Iterable<Cookie> cookies)
jozh
  • 2,412
  • 2
  • 15
  • 15
1

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=/")

Community
  • 1
  • 1
Anton Bessonov
  • 9,208
  • 3
  • 35
  • 38
0

There is one work-arround.

req.response()
.putHeader("Set-Cookie", "some=cookie;max-age=1000;path=/;HttpOnly"
    +"\nSet-Cookie: next=cookie"
    +"\nSet-Cookie: nnext=cookie;HttpOnly");
kipid
  • 585
  • 1
  • 8
  • 19