为什么在golang中curl命令不起作用?

huangapple go评论85阅读模式
英文:

Why is the curl command doesn't work in golang

问题

我在使用golang运行curl命令时遇到了麻烦。我的golang版本是1.15.7。目的是使用POST方法调用一个API。这是我想要使用的完整命令:

curl -X POST -H "Content-Type:application/json" -d '{"DNS_ID":"DNS_1","Domain_ID":"Domain_1","Cell_ID":"Cell_1","Device_ID":"IOT1","IMEI":"ims-208930000000003","IPv4":"60.60.0.1","IPv6":":::60.60.0.1","Slice_ID":"121312","FQDN":"test.free5gc"}' "http://192.168.1.233:5534/current" -v

这是我目前的代码。我在golang中使用了os/exec模块来运行curl命令。

var body string
var FQDN string
var content string
var url string

content = "\"Content-Type:application/json\""
url = "\"http://192.168.1.233:5534/current\""
DomainName := "test"
IPAddress := "60.60.0.1"

FQDN = DomainName + ".free5gc"
body = `"DNS_ID":"DNS_1","Domain_ID":"Domain_1","Cell_ID":"Cell_1","Device_ID":"IOT1","IMEI":"ims-208930000000003","IPv4":"` + IPAddress + `","IPv6":":::` + IPAddress + `","Slice_ID":"121312","FQDN":"` + FQDN + `"`

fmt.Println(body)

c := exec.Command("curl", "-X", "POST", "-H", content, "-d", "'{"+ body +"}'", url, "-v")

fmt.Println(c)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err := c.Run()
if err != nil {
    fmt.Println("Error: ", err)
}

在构建并运行代码后,整个命令是正确的,但似乎在url部分有一些问题。错误消息如下所示:

/usr/bin/curl -X POST -H "Content-Type:application/json" -d '{"DNS_ID":"DNS_1","Domain_ID":"Domain_1","Cell_ID":"Cell_1","Device_ID":"IOT1","IMEI":"ims-208930000000003","IPv4":"60.60.0.1","IPv6":":::60.60.0.1","Slice_ID":"121312","FQDN":"test.free5gc"}' "http://192.168.1.233:5534/current" -v
* Could not resolve host: "http
* Closing connection 0
curl: (6) Could not resolve host: "http
Error: exit status 6

我还在我的虚拟机中尝试了与上述命令相同的命令,curl命令可以正常工作。所以我想知道这是否是golang的正确方式。

我不知道错误发生在哪里。也许是"""\"content\""这些地方出了问题。错误消息似乎无法识别整个URL。这只是我的猜测。

希望大家能帮助我解决这个问题。非常感谢!

英文:

I am in the trouble when I use golang to run the command curl. My version of the golang is 1.15.7. The purposed is to call a API by using POST method. This is the whole command that I want to use:

curl -X POST -H "Content-Type:application/json" -d '{"DNS_ID":"DNS_1","Domain_ID":"Domain_1","Cell_ID":"Cell_1","Device_ID":"IOT1","IMEI":"ims-208930000000003","IPv4":"60.60.0.1","IPv6":":::60.60.0.1","Slice_ID":"121312","FQDN":"test.free5gc"}' "http://192.168.1.233:5534/current" -v

And this is my code so far. I used module os/exec in golang to run the command curl.

	var body string
	var FQDN string
	var content string
	var url string

	content = "\"Content-Type:application/json\""
	url ="\"http://192.168.1.233:5534/current\""
	DomainName := "test"
	IPAddress := "60.60.0.1"

	FQDN = DomainName + ".free5gc"
	body = `"DNS_ID":"DNS_1","Domain_ID":"Domain_1","Cell_ID":"Cell_1","Device_ID":"IOT1","IMEI":"ims-208930000000003","IPv4":"`+ IPAddress +`","IPv6":":::`+ IPAddress +`","Slice_ID":"121312","FQDN":"`+FQDN  +`"`

	fmt.Println(body)

	c := exec.Command("curl","-X","POST","-H",content,"-d","'{"+ body +"}'",url,"-v")

	fmt.Println(c)
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	err := c.Run()
	if err != nil {
		fmt.Println("Error: ", err)
	}

After I build the code and run it. The whole command is correct but seems it has some problem in the part of the url. The error message is like below:

/usr/bin/curl -X POST -H "Content-Type:application/json" -d '{"DNS_ID":"DNS_1","Domain_ID":"Domain_1","Cell_ID":"Cell_1","Device_ID":"IOT1","IMEI":"ims-208930000000003","IPv4":"60.60.0.1","IPv6":":::60.60.0.1","Slice_ID":"121312","FQDN":"test.free5gc"}' "http://192.168.1.233:5534/current" -v
* Could not resolve host: "http
* Closing connection 0
curl: (6) Could not resolve host: "http
Error:  exit status 6

Also I have tried the command which is same as the above one in my VM. And the command curl can work. So I was wondering if it is the write way of the golang or not.
I have no idea where the error occur. Maybe it is the "",` or ""content"" I thought. The error message seems can't recognize the whole URL. This is just mu guess.
Hope guys can help me to deal with this problem.
Thanks a lot

答案1

得分: 2

这是因为引号是bash用来组织参数的一种方式,而你正在使用Golang的os/exec包,所以在执行命令之前不需要转义参数(除非你在go和你要运行的程序之间使用了bash):

var body string
var FQDN string
var content string
var url string

content = "Content-Type: application/json"
url = "http://192.168.1.233:5534/current"
DomainName := "test"
IPAddress := "60.60.0.1"

FQDN = DomainName + ".free5gc"
body = "\"DNS_ID\":\"DNS_1\",\"Domain_ID\":\"Domain_1\",\"Cell_ID\":\"Cell_1\",\"Device_ID\":\"IOT1\",\"IMEI\":\"ims-208930000000003\",\"IPv4\":\"" + IPAddress + "\",\"IPv6\":\":::" + IPAddress + "\",\"Slice_ID\":\"121312\",\"FQDN\":\"" + FQDN + "\""

fmt.Println(body)

c := exec.Command("curl","-X","POST","-H",content,"-d","{"+ body +"}",url,"-v")

fmt.Println(c)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err := c.Run()
if err != nil {
    fmt.Println("Error: ", err)
}

tl;dr: 删除引号一切应该正常工作 :)

<details>
<summary>英文:</summary>

This is happening because quotes are a way for `bash` to organise arguments, and since you are using Golang&#39;s `os/exec` package, there&#39;s no need to escape arguments prior to the command&#39;s execution (unless you are using `bash` in between go and the program you&#39;re trying to run):

    var body string
    var FQDN string
    var content string
    var url string

    content = &quot;Content-Type: application/json&quot;
    url = &quot;http://192.168.1.233:5534/current&quot;
    DomainName := &quot;test&quot;
    IPAddress := &quot;60.60.0.1&quot;

    FQDN = DomainName + &quot;.free5gc&quot;
    body = `&quot;DNS_ID&quot;:&quot;DNS_1&quot;,&quot;Domain_ID&quot;:&quot;Domain_1&quot;,&quot;Cell_ID&quot;:&quot;Cell_1&quot;,&quot;Device_ID&quot;:&quot;IOT1&quot;,&quot;IMEI&quot;:&quot;ims-208930000000003&quot;,&quot;IPv4&quot;:&quot;`+ IPAddress +`&quot;,&quot;IPv6&quot;:&quot;:::`+ IPAddress +`&quot;,&quot;Slice_ID&quot;:&quot;121312&quot;,&quot;FQDN&quot;:&quot;`+FQDN  +`&quot;`

    fmt.Println(body)

    c := exec.Command(&quot;curl&quot;,&quot;-X&quot;,&quot;POST&quot;,&quot;-H&quot;,content,&quot;-d&quot;,&quot;{&quot;+ body +&quot;}&quot;,url,&quot;-v&quot;)

    fmt.Println(c)
    c.Stdout = os.Stdout
    c.Stderr = os.Stderr
    err := c.Run()
    if err != nil {
        fmt.Println(&quot;Error: &quot;, err)
    }

tl;dr: drop the quotes, everything should start working :)

</details>



huangapple
  • 本文由 发表于 2021年9月24日 16:35:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/69311943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定