I'm currently attempting to debug an issue with issuing a HTTP POST via GoLang 1.6.
The connection is failing with an 'EOF' error.
The test client code: https://gist.github.com/fatmcgav/35d1a4fbd74c7208f445c487f756a5e1
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"os"
)
func main() {
const body = "{\"auth\":" +
"{\"identity\":" +
"{\"methods\":[\"password\"]," +
"\"password\":" +
"{\"user\":{\"name\":\"xxx\"," +
"\"password\":\"xxx\"," +
"\"domain\":{\"name\":\"xxx_domain\"}" +
"}" +
"}" +
"},\"scope\":{\"project\":" +
"{\"domain\": " +
"{\"name\":\"xxx_domain\"},\"name\":\"xxx\"}}}}"
// Unmarshall JSON
var m map[string]interface{}
json.Unmarshal([]byte(body), &m)
// fmt.Println("%v", m)
var req *http.Request
var resp *http.Response
var dump []byte
var err error
client := &http.Client{}
url := "https://identity.xxx/v3/auth/tokens"
fmt.Println("Requesting auth token against URL: %s", url)
rendered, err := json.Marshal(m)
if err != nil {
fmt.Println("Error marshalling body: %q", err)
os.Exit(2)
}
req, err = http.NewRequest("POST", url, bytes.NewReader(rendered))
if err != nil {
fmt.Println("Got an error: %q", err)
os.Exit(2)
}
// Need to Close connection
req.Close = true
fmt.Println("Setting Insecure TLS mode")
// Configure custom TLS settings.
config := &tls.Config{InsecureSkipVerify: true}
transport := &http.Transport{TLSClientConfig: config,
DisableKeepAlives: true,
DisableCompression: true,
}
client.Transport = transport
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
dump, err = httputil.DumpRequestOut(req, true)
fmt.Printf("Outgoing requst =
%q
", dump)
resp, err = client.Do(req)
if err != nil {
fmt.Println("Error encountered: %q", err)
}
dump, err = httputil.DumpResponse(resp, true)
fmt.Printf("Response =
%q
", dump)
}
and resultant output is here:
~/golang/scripts go build req-test.go && ./req-test
Requesting auth token against URL: %s https://identity.xxx/v3/auth/tokens
Setting Insecure TLS mode
Outgoing requst =
"POST /v3/auth/tokens HTTP/1.1
Host: identity.xxx
User-Agent: Go-http-client/1.1
Connection: close
Content-Length: 262
Accept: application/json
Content-Type: application/json
Accept-Encoding: gzip
{\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"domain\":{\"name\":\"BusinessSupport_domain\"},\"name\":\"xxx\",\"password\":\"xxx\"}}},\"scope\":{\"project\":{\"domain\":{\"name\":\"xxx_domain\"},\"name\":\"xxx\"}}}}"
Error encountered: %q Post https://identity.xxx/v3/auth/tokens: EOF
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x13659e]
goroutine 1 [running]:
panic(0x338980, 0xc82000a170)
/usr/local/opt/go/libexec/src/runtime/panic.go:464 +0x3e6
net/http/httputil.DumpResponse(0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0)
/usr/local/opt/go/libexec/src/net/http/httputil/dump.go:285 +0x42e
main.main()
/Users/gavinw/golang/scripts/req-test.go:77 +0xdc
For obvious reasons, I've anonymised some of the details..
Any pointers on what I'm doing wrong..
Cheers
Gavin