Copying Body from one http.Request to another

1,450 views
Skip to first unread message

Ken GoNoob

unread,
Feb 4, 2014, 5:34:33 PM2/4/14
to golan...@googlegroups.com
I need to receive an http request, modify the XML body slightly, and resend the request to another server. I'm receiving the original request (inreq), am able to 
dump the body and verify the contents, and then create a new request, like this:

    newreq := http.NewRequest(inreq.Method, dest, inreq.Body)
    newreq.Close = true
    newreq.Body = inreq.Body

    resp, err1 := client.Do(newreq)

however I get an error: http: Request.ContentLength=596 with Body length 0

Is there a different way I should be copying the body to newreq? Related, I also want to modify the body, but it's type is called an "io.ReaderCloser". I'm not sure how I can convert this to a string, modify, and get it back to being a ReaderCloser.

Suggestions appreciated...
Ken



Dave Cheney

unread,
Feb 4, 2014, 5:47:23 PM2/4/14
to Ken GoNoob, golan...@googlegroups.com


On 5 Feb 2014, at 9:34, Ken GoNoob <drke...@gmail.com> wrote:

I need to receive an http request, modify the XML body slightly, and resend the request to another server. I'm receiving the original request (inreq), am able to 
dump the body and verify the contents, and then create a new request, like this:

Once the body is read, it can't be read again. Use ioutil.ReadAll to pull the body into a []byte. 



    newreq := http.NewRequest(inreq.Method, dest, inreq.Body)
    newreq.Close = true
    newreq.Body = inreq.Body

    resp, err1 := client.Do(newreq)

however I get an error: http: Request.ContentLength=596 with Body length 0

Is there a different way I should be copying the body to newreq? Related, I also want to modify the body, but it's type is called an "io.ReaderCloser". I'm not sure how I can convert this to a string, modify, and get it back to being a ReaderCloser.

Read the body as described above or use io.Copy to copy it into a bytes.Buffer then call String()


Suggestions appreciated...
Ken



--
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...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages