Setting Cookies in http Response

1,523 views
Skip to first unread message

laxman.v...@gmail.com

unread,
Apr 25, 2016, 5:41:01 AM4/25/16
to golang-nuts
Hi All,

I have a situation to create a http response and return to a function whose return type should be response(not response writer).

newrsp = &http.Response{
                                                StatusCode: 200,
                                                Body:       ioutil.NopCloser(strings.NewReader("my string")),
                                        }
By using above method I can set the headers of http and also body. But is there is a way to set a cookie without Writer.

Thanks & Regards,
L Vallandas

Ilya Kostarev

unread,
Apr 25, 2016, 7:46:39 AM4/25/16
to golan...@googlegroups.com
Seems to me
Coocie lives in http.Response.Header(which is a map[string][]string) with key "Set-Cookie". After constructing http.Cookie(which is a struct) or getting existent  one way you serialize it to string with *Coocie.String() and add to Response.Header with responce.Header.Add("Set-Cookie", *coocie.String())
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

msilv...@cloudflare.com

unread,
Apr 25, 2016, 4:24:53 PM4/25/16
to golang-nuts
> Coocie lives in http.Response.Header(which is a map[string][]string) with key "Set-Cookie". After constructing http.Cookie(which is a struct) or getting existent  one way you serialize it to string with *Coocie.String() and add to Response.Header with responce.Header.Add("Set-Cookie", *coocie.String())

Just call http.SetCookie(w, cookie) - https://golang.org/pkg/net/http/#SetCookie

laxman.v...@gmail.com

unread,
Apr 26, 2016, 2:14:06 AM4/26/16
to golang-nuts, msilv...@cloudflare.com

Adding Set-Cookie to response.Header crashes. Headers can be added to Response headers using response.Header.Add() except cookies.
http://play.golang.org/p/Gwtc0rXQka

newrsp.Header.Add("Set-Cookie", sslCookie.String()) ---> panics with error:panic serving 127.0.0.1:40698: assignment to entry in nil map

@
msilv, problem is not just setting the cookie. its about possibility of setting the cookie in http response without using http response writer.

Jesse McNelis

unread,
Apr 26, 2016, 3:43:43 AM4/26/16
to laxman.v...@gmail.com, golang-nuts, msilv...@cloudflare.com
On Tue, Apr 26, 2016 at 4:13 PM, <laxman.v...@gmail.com> wrote:
>
> Adding Set-Cookie to response.Header crashes. Headers can be added to
> Response headers using response.Header.Add() except cookies.
> http://play.golang.org/p/Gwtc0rXQka
> newrsp.Header.Add("Set-Cookie", sslCookie.String()) ---> panics with
> error:panic serving 127.0.0.1:40698: assignment to entry in nil map
>

This crashes because response.Header is nil. You need to initialize it first.
eg. newrsp.Header = http.Header{}

Your use of http.SetCookie() also doesn't work because you're sending
the response body first, preventing any further headers from being
added.

This example works, it does set the cookie twice though.
https://play.golang.org/p/tKjt44y_5b
Reply all
Reply to author
Forward
0 new messages