I think what people are trying to explain is that the behavior you may be expecting based on experience with browsers is not built in to the Go (or many other) HTTP clients. Even the Java cookie jar which has been mentioned in this thread was not available in early implementations of Java HTTP client libraries. If you want a cookie that the client receives in a response to be sent in subsequent HTTP requests, then you will must construct those requests to attach the cookie.
comments that have been posted refer On Wednesday, 12 September 2018 19:45:20 UTC-7, Weeds Qian wrote: > > If you look at my sample code, you should know I only send one request by > http.POST, the following request is done by golang http client automaticly, > not issue by myself. > > You got the point , why the http client do that without cookie from last > response, that's what I am asking > > 在 2018年9月12日星期三 UTC+8下午2:52:20,Volker Dobler写道: >> >> On Wednesday, 12 September 2018 06:28:12 UTC+2, Weeds Qian wrote: >>> >>> What did you expect to see? >>> >>> the cookie in response >>> What did you see instead? >>> >>> no cookie in response >>> >>> >>> ------------------http tcp stream from wireshark >>> ----------------------------- >>> >>> You can see that I only do the POST /login and the later is execute by >>> http client implementation. >>> >>> From my point of view, I think the GET /home.html request should add >>> the cookie from last response, but it didn't, since you don't do that, why >>> don't just return the response to me instead of doing useless request cause >>> we go the login page. >>> >>> POST /login HTTP/1.1 >>> Host: 127.0.0.1:8081 >>> User-Agent: Go-http-client/1.1 >>> Content-Length: 28 >>> Content-Type: application/x-www-form-urlencoded >>> Accept-Encoding: gzip >>> >>> username=admin&password=testHTTP/1.1 302 Found >>> Location: /home.html >>> Set-Cookie: >>> user=MTUzNjY1NjA2MXxEdi1CQkFFQ180SUFBUkFCRUFBQUpfLUNBQUVHYzNSeWFXNW5EQW9BQ0hWelpYSnVZVzFsQm5OMGNtbHVad3dIQUFWaFpHMXBiZz09fKI-QQWYHP_ZywpgkIoDmTzL1eJhd7pk-i9FSUgwI89E; >>> Path=/; HttpOnly >>> Date: Tue, 11 Sep 2018 08:54:21 GMT >>> Content-Length: 0 >>> >>> >> There is your cookie. >> >> The problem is not that the cookie is not part of the response >> you get from /login but that you do not keep the cookie and >> send it on subsequent requests: >> >>> >>> GET /home.html HTTP/1.1 >>> Host: 127.0.0.1:8081 >>> User-Agent: Go-http-client/1.1 >>> Content-Type: application/x-www-form-urlencoded >>> Referer: http://127.0.0.1:8081/login >>> Accept-Encoding <http://127.0.0.1:8081/loginAccept-Encoding>: gzip >>> >>> >> You see: No Cookie header here. >> >> To record Set-Cookie headers and generate appropriate Cookie headers >> from these set cookies you probably should use your own instance of >> net/http.Client with a non-nil Jar and use that client to Do a POST >> request. >> You can create a Jar via net/http/cookiejar.New. >> If you are going to use this on arbitrary domains please consider setting >> the Jar's PublicSuffixList to e.g. golang.org/x/net/publicsuffix.List >> >> V. >> >> -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.