英文:
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's `os/exec` package, there's no need to escape arguments prior to the command's execution (unless you are using `bash` in between go and the program you're trying to run):
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: drop the quotes, everything should start working :)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论