这里使用golang发送post请求获取zabbix API的token


 

package main



import (

    "bytes"

    "fmt"

    "io/ioutil"

    "net/http"

)



func httpPostJson() {

    jsonStr :=[]byte(`{"jsonrpc": "2.0",

    "method": "user.login",

    "params": {
    
    "user": "Admin",

    "password": "zabbix"

},

"id": 0

}`)

url:= "http://119.3.169.169/api_jsonrpc.php"

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

req.Header.Set("Content-Type", "application/json")



client := &http.Client{}

resp, err := client.Do(req)

if err != nil {

// handle error

}

defer resp.Body.Close()



statuscode := resp.StatusCode

hea := resp.Header

body, _ := ioutil.ReadAll(resp.Body)

fmt.Println(string(body))

fmt.Println(statuscode)

fmt.Println(hea)



}



func main() {

httpPostJson()

}package main



import (

    "bytes"

    "fmt"

    "io/ioutil"

    "net/http"

)



func httpPostJson() {

jsonStr :=[]byte(`{"jsonrpc": "2.0",

"method": "user.login",

"params": {

"user": "Admin",

"password": "zabbix"

},

"id": 0

}`)

url:= "http://119.3.169.169/api_jsonrpc.php"

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

req.Header.Set("Content-Type", "application/json")



client := &http.Client{}

resp, err := client.Do(req)

if err != nil {

// handle error

}

defer resp.Body.Close()



statuscode := resp.StatusCode

hea := resp.Header

body, _ := ioutil.ReadAll(resp.Body)

fmt.Println(string(body))

fmt.Println(statuscode)

fmt.Println(hea)



}



func main() {

httpPostJson()

}