FAQ
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

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ilya Kostarev at Apr 25, 2016 at 11:46 am
    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())
    On 04/25/2016 12:40 PM, laxman.vallandas@gmail.com wrote:
    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
    --
    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+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Msilverlock at Apr 25, 2016 at 8:24 pm
    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

    On Monday, April 25, 2016 at 4:46:39 AM UTC-7, Uvelichitel wrote:

    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())
    On 04/25/2016 12:40 PM, laxman.v...@gmail.com <javascript:> wrote:

    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
    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/d/optout.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Laxman Vallandas at Apr 26, 2016 at 6:13 am
    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.



    On Tuesday, 26 April 2016 01:54:53 UTC+5:30, msilv...@cloudflare.com wrote:

    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

    On Monday, April 25, 2016 at 4:46:39 AM UTC-7, Uvelichitel wrote:

    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())
    On 04/25/2016 12:40 PM, laxman.v...@gmail.com wrote:

    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
    --
    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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jesse McNelis at Apr 26, 2016 at 7:43 am

    On Tue, Apr 26, 2016 at 4:13 PM, 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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 25, '16 at 9:40a
activeApr 26, '16 at 7:43a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase