我目前正在尝试通过GoLang 1.6发布HTTP POST来调试问题。
连接失败并显示“EOF”错误。
测试客户端代码: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 = \n%q\n", dump)
resp, err = client.Do(req)
if err != nil {
fmt.Println("Error encountered: %q", err)
}
dump, err = httputil.DumpResponse(resp, true)
fmt.Printf("Response = \n%q\n", dump)
}
结果输出在这里:
~/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\r\nHost: identity.xxx\r\nUser-Agent: Go-http-client/1.1\r\nConnection: close\r\nContent-Length: 262\r\nAccept: application/json\r\nContent-Type: application/json\r\nAccept-Encoding: gzip\r\n\r\n{\"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
出于显而易见的原因,我已经匿名了一些细节......
关于我做错的任何指示......
干杯
加文