New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net/http: redirection response has closed body #10069
Comments
Sorry, I'm just starting a two-week vacation now. I won't be able to look into this until the 19th or later. Maybe somebody else can in the meantime. |
No worries; it's easy to work around. I'm just curious if this was intended behavior or not. I can't quite decide if it's exactly a bug, so I figured I'd bring it up just in case it was. |
Too late for Go 1.5. |
Why is this body closed with a redirect? I'm writing a curl-like go executable and curl will, by default return a 301/302 response with applicable headers (Location, obviously) and the response body unless you tell it to follow. Currently I'm able to tell it to not follow the redirects by default, but the body is gone and I have to use the hack above to prevent my program from exiting. |
Is there any change in decision when this issue will be fixed? I had to make custom code around http.Client and http.Transport for block redirect and fetch response body of the redirect and i am not happy with the workaround. |
Nobody looked at it during the Go 1.6 cycle, either, and I was only looking at the Go1.6 label (not Unplanned) myself. Let's tag it Go1.7 and then I will at least will see it when the 1.7 tree opens. Others are more than welcome to send fixes too. |
I am seeing similar issue. I also think the current required workaround is quite kludgy and really doesn't work for me. Ideally, there should be some intuitive mechanism to disable redirects entirely and allow parsing of the raw response. In my case, I want to simply reflect the server response AS IS back to the client that made the initial request (I have implemented a custom proxy while munging stuff in the middle). The workaround didn't seem to get me what I needed. I saw an HTTP 200 response with a Content-Length of 0 :( But it's also possible I am doing something very wrong as I am new to golang... Edit: Just a note that I would expect golang net/http to function very much like curl or most browsers and currently it seems to be VERY different from that. I was incredibly surprised to see my custom headers disappear on redirect to the same server. That is not how your browser would respond. If you send a Cookie header to a server like http://example.com/home as a client, the client will still send that Cookie again following a 3xx redirect to http://example.com/home/defaultpage.html. I don't see why this wouldn't be the case with golang net/http. |
I was going to submit a patch, but need some clarification. The preconditions we have are:
Two possible solutions:
|
In relation to #8633, CL https://go-review.googlesource.com/#/c/21364/ documented that during a redirection, on encountering a CheckRedirect error, the response is non-nil and its body is always closed. @bradfitz is that relevant to this issue? |
@jbardin, sorry for the slow reply. Yes, we can't really change the old behavior. As @odeke-em pointed out, for #8633 I just documented the status quo in https://golang.org/cl/21364. Some possibilities I see for this bug:
I think it's probably too late for major new API surface in Go 1.7, sadly, so I think the third option is out (and I don't like having two so-similar things anyway), but I think the |
That sounds good, combining your first two bullet points into a solution: magic return value from |
CL https://golang.org/cl/23207 mentions this issue. |
go version go1.4.2 darwin/amd64
Not really relevant to this issue...
MacBook Pro (15-inch, Mid 2012)
2.6 GHz Intel Core i7
16 GB 1600 MHz DDR3
Implemented a custom redirection policy handler via
http.Client
'sCheckRedirect
to stop redirection.After a
Do()
call on a request, the returned response'sBody
is not nil and reading from it returns an errorhttp: read on closed response body
. See example code below.Expected either an unclosed, unread-from non-nil
Body
, or anil
Body
.Saw a closed, already-read-from
Body
that was notnil
.This unexpected condition required me to hack around the case by explicitly setting
resp.Body
tonil
when detecting my custom redirect policy abort error from the returnedurl.Error
.Also, I have no access to the intermediate redirection responses. I would want an interception function to be able to inspect the redirection response to see if I should redirect or not, but instead I get a request instance; what good is the request to determine if I should redirect? This design also prevents me from logging relevant redirection details to the end user, so as to properly trace the request-response path.
The text was updated successfully, but these errors were encountered: