-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: Request.Clone() does not deep copy Body contrary to its docs (using GetBody works though) #36095
Comments
Body
)Body
)
Body
)
What I did to solve it is use the same approach as in
To reiterate, I'm happy to submit a PR, whether it be documentation or code, but I would like to know which one first to avoid spending time on something not wanted! |
Thank you for reporting this issue @nicolascouvrat and welcome to the Go project! So, given that Request.Body is an io.ReadCloser, there aren't guarantees for being clonable or rewindable. For the longest time in Go, we didn't even have GetBody (introduced in Go1.8) and What I think that we can do here is update the docs for Clone to indicate that Request.Body will not be cloned, but that GetBody can be used for this purpose. |
Change https://golang.org/cl/212408 mentions this issue: |
Hi @odeke-em and thanks for the insight. I'll change the docs in the upcoming few days when I'll have time, I guess it will make for a good introduction/first PR for me :)
Could you elaborate a little? Sorry if this is obvious, but I am a little confused. Is cloning an From what I get of your comment (which makes a lot of sense, I've had a feeling that the Thanks! |
@nicolascouvrat i think the solution is to return this instead |
Hi @nicolascouvrat. I am also trying to replace the context in a request. I tried your code in a middleware but it doesn't seem to copy the body:
What am I missing? |
@J7mbo looks like your debugging code is the culprit. After you read the body the first time to print it, the second read reads nothing (all input was already consumed).
^^ the body is empty after it has been read by ioutil.ReadAll. |
@rhcarvalho Thanks for finding that! That's a good few hours saved I reckon :) Sorry to hijack the thread! |
POST Bodyでも管理できるようにしました。今はcrawler側でPostFormのセットが必要ですが、可能なら無くしたいと思っています。
you forgot |
just ran into this. I agree that either Clone should clone the body, or it should be documented that it does not clone the body. as others said workarounds are GetBody or DumpRequest |
The Request.Clone() function is misleading, it does not create a copy of the original request's body, resulting in dumprequest+printf draining the original request's body. See golang/go#36095
The Request.Clone() function is misleading, it does not create a copy of the original request's body, resulting in dumprequest+printf draining the original request's body. See golang/go#36095
The Request.Clone() function is misleading, it does not create a copy of the original request's body, resulting in dumprequest+printf draining the original request's body. See golang/go#36095
The Request.Clone() function is misleading, it does not create a copy of the original request's body, resulting in dumprequest+printf draining the original request's body. See golang/go#36095
## What this PR does / why we need it: The `Request.Clone()` function is misleading, it does not create a copy of the original request's body, resulting in dumprequest+printf draining the original request's body. See golang/go#36095 ## Which issue(s) this PR fixes: ## Special notes for your reviewer: ## Does this PR introduce a user-facing change? ``` No. ```
The |
Change https://go.dev/cl/593175 mentions this issue: |
Fixes golang#36095 Change-Id: I94ae014b0ee45b4aeb38cb247e42cfc13f663ded Reviewed-on: https://go-review.googlesource.com/c/go/+/593175 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
I am currently using the latest release afaik.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
When the bug happens, I was playing with
httputil.DumpRequest
, but I managed to reproduce it with a shorter example:What did you expect to see?
I expected
Clone
to return a deep copy, such as I can do whatever i want with the clone body without affecting the original request.The documentation states:
What did you see instead?
The original request body is drained, and the code errors with
I think it comes from here where a shallow copy is done.
I feel like the reason why I want
Clone
to indeed be deep is not mainstream enough to warrant modifyingClone
(I want to save the request object as it is in aDebug
struct to inspect it later), and the cost could be big if the body is. However, I think it would be a good idea to update the documentation? I am happy to do a PR in this case.EDIT: Doing:
solves my initial problem, which was that
httputil.DumpRequestOut()
essentially destroyed theBody
of the argument. Maybe that function should be updated to useGetBody()
instead? I would be happy to make a second issue and PR for that one too.I still think the documentation problem for
Clone
stands though.The text was updated successfully, but these errors were encountered: