Body io.ReadCloser
It seems like the only way is to substitute a Body
with an object appropriate to io.ReadCloser
interface. For this purpose you need to construct a Body
object using any function which returns io.Reader
object and ioutil.NopCloser
to turn the io.Reader
object to io.ReadCloser
.
bytes.NewBufferString
NewBufferString creates and initializes a new Buffer using string s as its initial contents. It is intended to prepare a buffer to read an existing string.
strings.NewReader
(used in an example below)
NewReader returns a new Reader reading from s. It is similar to bytes.NewBufferString but more efficient and read-only.
ioutil.NopCloser
NopCloser returns a ReadCloser with a no-op Close method wrapping the provided Reader r.
new_body_content := "New content."
r.Body = ioutil.NopCloser(strings.NewReader(new_body_content))
Related attributes
But to be clear and not to confuse other parts of the application you need to change Request
attributes related to Body
content. Most of the time it is only ContentLength
:
r.ContentLength = int64(len(new_body_content))
And in a rare case, when your new content uses different encodings from original Body
, it could be TransferEncoding
.