-
Notifications
You must be signed in to change notification settings - Fork 9
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
add Request.CloneWithBody and Request.SetBody #105
Conversation
I wonder if we should also have a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just made the change in my code to use Clone yesterday, and almost forgot the post body. This is timely!
I prefer your suggestion of exposing the type, with a clear doc string explaining how it enables the |
@cee-dub My concern about adding |
0e1edf4
to
00fceaf
Compare
I pushed a replacement commit that adds |
These methods wrap provided `io.Reader`s that aren't already also closers in our own `nopCloser` type, which we already do in NewRequest. This extends our ability to send `Content-Length` with certain in-memory types to other uses of requests. We note in the documentation now that using `SetBody` is preferred over setting the `Body` field directly.) Fixes #104
00fceaf
to
74a7e6e
Compare
// If the body is set from an in-memory reader such as [bytes.Buffer], | ||
// [bytes.Reader], or [strings.Reader], the runtime will send the | ||
// request with a Content-Length header instead of Transfer-Encoding: | ||
// chunked. | ||
func (req *Request) SetBody(body io.Reader) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of a learning question here, but is it possible to have the case of an incoming request with a content-length brought to this body assignment as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realized this isn't super clear, but I think Clone
already handles this by copying the header if present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but the runtime will remove Content-Length
and Transfer-Encoding
before sending the request unless ManualFramingMode
is true.
// CloneWithBody returns a copy of the request, with the Body field set | ||
// to the provided io.Reader. Its URL will have a nil User field. | ||
func (req *Request) CloneWithBody(body io.Reader) *Request { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this could be removed now, perhaps by including the two lines of code into the doc of SetBody
.
These methods wrap provided
io.Reader
s that aren't already also closers in our ownnopCloser
type, which we already do in NewRequest. This extends our ability to sendContent-Length
with certain in-memory types to other uses of requests.We note in the documentation now that using
SetBody
is preferred over setting theBody
field directly.)Fixes #104