0

I am using a nanoHttp server. Nothing I have tried will work. I need to be able to click on one link (http://localhost:9090/createCookie) that calls a method to create a cookie for a different link (http://localhost:9090/application).

NanoHttp cookies do not support setting a path which is why I can't use them

Vickie
  • 89
  • 1
  • 9

1 Answers1

2

You can use the CookieHandler, from your NanoHTTPD instance:

@Override
public Response serve(IHTTPSession session) {
    // ...
    CookieHandler ch = new CookieHandler(session.getHeaders());
    ch.set("cookieName", "encoded cookie value", nDaysValid);
    // ...
    Response response = ...; // Build up response
    ch.unloadQueue(response); // Add cookies to it
    return response;
}

What do you mean by NanoHttp cookies do not support setting a path?

Matthieu
  • 2,736
  • 4
  • 57
  • 87
  • 1
    I found that you have to add the cookies to the response at the end of the serve() like this: `Response response = newFixedLengthResponse(msg); ch.unloadQueue(response); return response;` – Alexey Ozerov Sep 15 '16 at 18:47