在golang中发送像请求一样的curl

I try send request like this in golang but with no result:

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"

How to do that?

I want to send data do Domoticz Home Automation System. Anser I got:

{
   "status" : "ERR"
}

but should be:

{
"status" : "OK",
"title" : "Update Device"
}

I try this code:

    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)

This works for me:

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

but I think it is not best way to do that.

Note that in your initial curl command, you missed the -X POST parameter.
The generated code would then be:

// 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()

Your curl command and Go code are almost completely unalike.

  1. Your Go sends a POST request, and curl a GET request.
  2. Your curl command sets an Accept header, your Go code doesn't.
  3. Your Go command sends a body, your curl command doesn't.
  4. Your curl command sends URL parameters, your Go code doesn't.

Your go code does the curl equivalent of:

curl -s -i -X POST -H "Accept: application/json" "http://192.168.1.183:8080/json.htm" -d "type=command&c=getauth&param=udevice&idx=9&nvalue=0&svalue=10;43;2"

The simplest way to emulate your curl command in Go is:

req, err := http.NewRequest("GET", "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 {
    panic(err)
}
req.Header.Add("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)