在golang中发送类似curl的请求

Gen*_*nzi -2 json curl go

我尝试在 golang 中发送这样的请求但没有结果:

curl -s -i -H "Accept: application/json" "http://192.168.1.183:8080/json.htm?type=command&c=getauth&param=udevice&idx=9&nvalue=0&svalue=10;43;2"
Run Code Online (Sandbox Code Playgroud)

怎么做?

我想向 Domoticz 家庭自动化系统发送数据。安瑟我得到:

{
   "status" : "ERR"
}
Run Code Online (Sandbox Code Playgroud)

但应该是:

{
"status" : "OK",
"title" : "Update Device"
}
Run Code Online (Sandbox Code Playgroud)

我尝试这段代码:

    b := bytes.NewBufferString("type=command&c=getauth&param=udevice&idx=9&nvalue=0&svalue=10;43;2")
    res, _ := http.Post("http://192.168.1.183:8080/json.htm", "Accept: application/json", b)
Run Code Online (Sandbox Code Playgroud)

Von*_*onC 5

请注意,在您的初始curl命令中,您错过了该-X POST参数。生成的代码
将是:

// Generated by curl-to-Go: https://mholt.github.io/curl-to-go

req, err := http.NewRequest("POST", "http://192.168.1.183:8080/json.htm?type=command&c=getauth&param=udevice&idx=9&nvalue=0&svalue=10;43;2", nil)
if err != nil {
    // handle err
}
req.Header.Set("Accept", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
    // handle err
}
defer resp.Body.Close()
Run Code Online (Sandbox Code Playgroud)