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

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

Why is the curl command doesn't work in golang

问题

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

  1. 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命令。

  1. var body string
  2. var FQDN string
  3. var content string
  4. var url string
  5. content = "\"Content-Type:application/json\""
  6. url = "\"http://192.168.1.233:5534/current\""
  7. DomainName := "test"
  8. IPAddress := "60.60.0.1"
  9. FQDN = DomainName + ".free5gc"
  10. 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 + `"`
  11. fmt.Println(body)
  12. c := exec.Command("curl", "-X", "POST", "-H", content, "-d", "'{"+ body +"}'", url, "-v")
  13. fmt.Println(c)
  14. c.Stdout = os.Stdout
  15. c.Stderr = os.Stderr
  16. err := c.Run()
  17. if err != nil {
  18. fmt.Println("Error: ", err)
  19. }

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

  1. /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
  2. * Could not resolve host: "http
  3. * Closing connection 0
  4. curl: (6) Could not resolve host: "http
  5. 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:

  1. 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.

  1. var body string
  2. var FQDN string
  3. var content string
  4. var url string
  5. content = "\"Content-Type:application/json\""
  6. url ="\"http://192.168.1.233:5534/current\""
  7. DomainName := "test"
  8. IPAddress := "60.60.0.1"
  9. FQDN = DomainName + ".free5gc"
  10. 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 +`"`
  11. fmt.Println(body)
  12. c := exec.Command("curl","-X","POST","-H",content,"-d","'{"+ body +"}'",url,"-v")
  13. fmt.Println(c)
  14. c.Stdout = os.Stdout
  15. c.Stderr = os.Stderr
  16. err := c.Run()
  17. if err != nil {
  18. fmt.Println("Error: ", err)
  19. }

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:

  1. /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
  2. * Could not resolve host: "http
  3. * Closing connection 0
  4. curl: (6) Could not resolve host: "http
  5. 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):

  1. var body string
  2. var FQDN string
  3. var content string
  4. var url string
  5. content = "Content-Type: application/json"
  6. url = "http://192.168.1.233:5534/current"
  7. DomainName := "test"
  8. IPAddress := "60.60.0.1"
  9. FQDN = DomainName + ".free5gc"
  10. 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 + "\""
  11. fmt.Println(body)
  12. c := exec.Command("curl","-X","POST","-H",content,"-d","{"+ body +"}",url,"-v")
  13. fmt.Println(c)
  14. c.Stdout = os.Stdout
  15. c.Stderr = os.Stderr
  16. err := c.Run()
  17. if err != nil {
  18. fmt.Println("Error: ", err)
  19. }
  20. tl;dr: 删除引号一切应该正常工作 :)
  21. <details>
  22. <summary>英文:</summary>
  23. 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):
  24. var body string
  25. var FQDN string
  26. var content string
  27. var url string
  28. content = &quot;Content-Type: application/json&quot;
  29. url = &quot;http://192.168.1.233:5534/current&quot;
  30. DomainName := &quot;test&quot;
  31. IPAddress := &quot;60.60.0.1&quot;
  32. FQDN = DomainName + &quot;.free5gc&quot;
  33. 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;`
  34. fmt.Println(body)
  35. 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;)
  36. fmt.Println(c)
  37. c.Stdout = os.Stdout
  38. c.Stderr = os.Stderr
  39. err := c.Run()
  40. if err != nil {
  41. fmt.Println(&quot;Error: &quot;, err)
  42. }
  43. tl;dr: drop the quotes, everything should start working :)
  44. </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:

确定