why i use http.Do func reply an error: with body length 0

1,511 views
Skip to first unread message

cheera...@gmail.com

unread,
Dec 27, 2013, 5:56:36 AM12/27/13
to golan...@googlegroups.com
I wrote a simple code for gcm service: 

   httpReq, err := http.NewRequest(`POST`, `https://android.googleapis. com/gcm/send`, bytes.NewReader(jsonStr))
   ... 
   
   httpReq.Header.Set(`Authorization`, `key=`+gcmKey.Key)              
   httpReq.Header.Set(`Content-Type`, `application/json`) 

   ...
   httpResp, err := http.DefaultClient.Do(httpReq)


But I checked my log after running this code, I found Do() replied an error: 
   Post https://android.googleapis.com/gcm/send: http: Request.ContentLength=326 with Body length 0

From the error, ContentLength=326, that means http.NewRequest() set the length correctly. And then I printed httpReq via Write() func, it has body definitely.

Anybody can help solve this? 


Jesse McNelis

unread,
Dec 27, 2013, 9:19:10 AM12/27/13
to cheera...@gmail.com, golang-nuts
On Fri, Dec 27, 2013 at 9:56 PM, <cheera...@gmail.com> wrote:
I wrote a simple code for gcm service: 

   httpReq, err := http.NewRequest(`POST`, `https://android.googleapis. com/gcm/send`, bytes.NewReader(jsonStr))
   ... 
   
   httpReq.Header.Set(`Authorization`, `key=`+gcmKey.Key)              
   httpReq.Header.Set(`Content-Type`, `application/json`) 

   ...
   httpResp, err := http.DefaultClient.Do(httpReq)


Showing bits of code is generally unhelpful. Since you don't know what the problem is you're not going to be able to know which parts of the code are important in figuring out what the problem is. If you post a complete example we might be of more help.
 
But I checked my log after running this code, I found Do() replied an error: 
   Post https://android.googleapis.com/gcm/send: http: Request.ContentLength=326 with Body length 0

From the error, ContentLength=326, that means http.NewRequest() set the length correctly. And then I printed httpReq via Write() func, it has body definitely.

My guess is that this is your problem, You're printing the request which is draining the io.Reader that the body of the request is in, and then you're doing a http.DefaultClient.Do(httpReq) and there is nothing left to read.
Removing the code that is printing the request should solve this.
Reply all
Reply to author
Forward
0 new messages