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:
P
ost 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,