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
CodePudding user response:
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 :)