golang send https request,panic: runtime error

-1

I'm new to golang.I want get some items through zabbix api.

code :

func zbxGetAuth() {
    url := "https://monitor.mydomian.com:5140/zabbix/api_jsonrpc.php"
    contentType := "application/json-rpc"
    data := []byte(`{"jsonrpc": "2.0","method": "user.login","params": {"user": "test","password": "pSkoHwJ8s"},"id": "1"}`)

    tr := &http.Transport{
        DisableCompression: true,
        TLSClientConfig:    &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}
    resp, err := client.Post(url, contentType, bytes.NewBuffer(data))
    if err != nil {
        fmt.Printf("post failed, err:%v\n", err)
    }
    defer resp.Body.Close()

    b, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("get resp failed,err:%v\n", err)
    }
    fmt.Println(string(b))
}

panic info:

post failed, err:Post http://localhost/zabbix/api_jsonrpc.php: dial tcp [::1]:80: connectex: No connection could be made because the target machine actively refused it.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x40 pc=0x6c24da]

goroutine 1 [running]:
zbxCheck/inits.zbxGetAuthID()
        C:/Users/jim/go/src/zbxCheck/inits/initZBXAuth.go:41 +0x22a
main.main()
        C:/Users/jim/go/src/zbxCheck/start.go:15 +0x81
exit status 2

why my url was been replaced with "http://localhost/zabbix/api_jsonrpc.php",and correct url is "https://monitor.mydomian.com:5140/zabbix/api_jsonrpc.php"?

go
zabbix
asked on Stack Overflow Mar 23, 2020 by uniquecats2020

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0