how to read an http request's body?

37,555 views
Skip to first unread message

Bill Yan

unread,
Apr 19, 2012, 2:26:28 AM4/19/12
to golan...@googlegroups.com
Hello guys,

I want to read the body of an http request. I have tried two ways, none of them worked.:


1. body, _ := ioutil.ReadAll(r.Body)



2. var hah []byte;
len,_ := r.Body.Read(hah);


Can you show me an example?

Thank you.

Brad Fitzpatrick

unread,
Apr 19, 2012, 2:47:57 AM4/19/12
to Bill Yan, golan...@googlegroups.com
On Wed, Apr 18, 2012 at 11:26 PM, Bill Yan <bill...@gmail.com> wrote:
Hello guys,

I want to read the body of an http request. I have tried two ways, none of them worked.:


1. body, _ := ioutil.ReadAll(r.Body)

Well, what was the error?  You're ignoring it with that underscore.
 
2. var hah []byte;
len,_ := r.Body.Read(hah);

Read the the docs on io.Reader.  This only reads "some" amount... not necessarily the full amount of you asked for.  See the bufio package too, and NewReader, if you care about reading lines.
 

Jan Mercl

unread,
Apr 19, 2012, 3:05:45 AM4/19/12
to golan...@googlegroups.com
On Thursday, April 19, 2012 8:26:28 AM UTC+2, Bill Yan wrote:
2. var hah []byte;
len,_ := r.Body.Read(hah);

Adding to what Brad said: Also ensure len(hah) != 0 before passing it to any .Read(), e.g. by hah := make([]byte, someSize).

Bill Yan

unread,
Apr 19, 2012, 12:26:32 PM4/19/12
to golan...@googlegroups.com
len is always 0. I tried. Which means, I probably haven't finish streaming the request's content. I can't find an example of reading the content of a request's body, only how to read form data.

 

Brad Fitzpatrick

unread,
Apr 19, 2012, 12:38:18 PM4/19/12
to Bill Yan, golan...@googlegroups.com
Show your code.

But before you show it, stop ignoring errors with _.

I bet you're reading the body (which you can only do once) and then you're trying to read it again and finding that it's empty.

ioutil.ReadAll is what you probably want.  Don't use Read unless you really know what you're doing.  (in particular, you need to give it a buffer to read into, and it isn't guaranteed to read more than a byte, assuming you give it a byte of space.)

Message has been deleted

Bill Yan

unread,
Apr 19, 2012, 3:20:39 PM4/19/12
to golan...@googlegroups.com
Thank you very much for your help.

Brad, you were right that I shouldn't ignore the error. 

This was my code:


func handleSignup(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "handle signup")
hah, err := ioutil.ReadAll(r.Body);
if err != nil {
fmt.Fprintf(w, "%s", err)
}

fmt.Fprintf(w,"%s",hah)
}

I found that the err says : invalid Read on closed request Body. 

After searching, I found an old post:


The reason for my issue is that, I shouldn't print to the http writer first. Because if I do that, the http request is closed. So I can't read anything from it.


Thank you for the help!

kh...@sycomore.vn

unread,
Sep 28, 2016, 8:25:56 AM9/28/16
to golang-nuts
Thanks man. 
I got same issue in your post. I call fmt.Fprintln before call json.NewDecoder.
I can run normally in develop server but when I deploy in App Engine, it stress me with " invalid Read on closed request Body"
Reply all
Reply to author
Forward
0 new messages