net/http POST error ?

328 views
Skip to first unread message

vlya...@gmail.com

unread,
Aug 9, 2015, 9:56:56 AM8/9/15
to golang-nuts
am trying to do (with net/http) equivalent  of the following curl call:
curl -i -k -X POST localhoost:443/myurl -H "X-SDS-AUTH-TOKEN: BAA.." -H "Content-Type: application/xml" -d@- <<EOF
<action_params>
       <param>dosomething</param>
</action_params>

My code is as following:
func main() {
    xmlstr := "<action_params> <param>dosomething</param> </action_params>

    r, _ := http.NewRequest("POST", "https://localhoost:443/myurl”, strings.NewReader(xmlstr))

    r.Header.Set("X-SDS-AUTH-TOKEN”, “BAA…”)
    r.Header.Set("Content-Type", "application/xml")
    //r.Header.Set("Content-Length", "310")
    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}
    client.Do(r)
    resp, err := client.Do(r)
    if err != nil {
        fmt.Printf("--> POST error %v", err)
        //return
    } else {
        defer resp.Body.Close()
        contents, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            fmt.Printf("%s", err)
            return
        }
        fmt.Printf("response=%v\n", resp)
        fmt.Printf("-->response status =%s\n", resp.Status)
    }
}

I have the following error:
Post https://127.0.0.1:4301/myurl: http: ContentLength=59 with Body length 0

I have tried to set content length in header r.Header.Set("Content-Length", "310”) but it didn’t make any difference…
Do I set request body incorrectly? Do I need to set body length in some header parameter?
Thanks,

Matt Harden

unread,
Aug 9, 2015, 11:27:15 AM8/9/15
to vlya...@gmail.com, golang-nuts
You're calling client.Do(r) twice. Remove the first instance of it.

--
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/d/optout.

Victor L

unread,
Aug 10, 2015, 8:01:40 AM8/10/15
to Matt Harden, golang-nuts
That's a typing error, i am actually calling it
once:
resp, err := client.Do(r)

Victor L

unread,
Aug 10, 2015, 8:16:09 AM8/10/15
to Matt Harden, golang-nuts
I just accidentally uncommented first call when i copied the snippet from editor. Still have the same problem  with single call:
Thank you,


func main() {
    xmlstr := "<action_params> <param>dosomething</param> </action_params>

    r, _ := http.NewRequest("POST", "https://localhoost:443/myurl”, strings.NewReader(xmlstr))

    r.Header.Set("X-SDS-AUTH-TOKEN”, “BAA…”)
    r.Header.Set("Content-Type", "application/xml")
    //r.Header.Set("Content-Length", "310")
    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}
   
    resp, err := client.Do(r)
    if err != nil {
        fmt.Printf("--> POST error %v", err)
        //return
    } else {
        defer resp.Body.Close()
        contents, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            fmt.Printf("%s", err)
            return
        }
        fmt.Printf("response=%v\n", resp)
        fmt.Printf("-->response status =%s\n", resp.Status)
    }
}
On Sun, Aug 9, 2015 at 11:26 AM, Matt Harden <matt....@gmail.com> wrote:

James Bardin

unread,
Aug 10, 2015, 10:12:25 AM8/10/15
to golang-nuts, matt....@gmail.com, vlya...@gmail.com


On Monday, August 10, 2015 at 8:16:09 AM UTC-4, Victor L wrote:
I just accidentally uncommented first call when i copied the snippet from editor. Still have the same problem  with single call:
Thank you,


The "Body length 0" error *was* from the extra http call, so you shouldn't have that any more.

Otherwise, you can't set the Content-Length to 310 when it is actually 59. The Transport will override you to create a valid http request.
Normally you don't set Content-Length at all, and let the transport determine what to use.

Žygimantas Stauga

unread,
Oct 8, 2015, 3:02:11 AM10/8/15
to golang-nuts, matt....@gmail.com, vlya...@gmail.com
I have this issue too, but even can't reproduce this. It happens "some times" without any code changes of course. I triple checked for draining byte reader. But no luck. Most of time everything works as expected, but some times failing, even had situation on production, I got this error, but other end actually received data... And I have no idea why...
Reply all
Reply to author
Forward
0 new messages