How to add string with quotes and slashes in golang

0

I'll share an example

I want the line below in golang string curl -u admin:admin -H 'Accept: application/yang-data+json' -s http://<ip>/restconf/data/ -v

code I wrote:

cmd := "curl -u admin:admin -H 'Accept: application/yang-data+json' -s http://" + ip_string + "/restconf/data/ -v"

err: unexpected string at the end of Line.

go
2021-11-23 16:07:26
1

1

unexpected string at the end of Line.

You can use fmt.Sprintf to format a string so that you don't have to stitch it together by hand. I find this easier to read and write, myself:

fmt.Sprintf("curl -u admin:admin -H 'Accept: application/yang-data+json' -s http://%s/restconf/data/ -v", ip_string)

Seems like you're trying to create a shell command to invoke Curl. Better than trying to escape your curl arguments for the shell, is to invoke curl directly. This way you can use Go to separate the arguments without having to worry about shell quoting:

cmd := exec.Command("curl", 
   "-u", "admin:admin",
   "-H", "Accept: application/yang-data+json",  
   "-s", 
   fmt.Sprintf("http://%s/restconf/data/", ip_string), 
   "-v",
)

However, if I were you, I'd use https://pkg.go.dev/net/http to make the request and obviate os/exec entirely. Performance and efficiency will be better, and handling the response and any error conditions will be way easier than doing that through curl and trying to parse output and handle error codes.

req, err := http.NewRequest("GET", fmt.Sprintf("http://%s", source_ip), nil)
// handle err
req.Header.Add("Accept", "application/yang-data+json")
req.SetBasicAuth("admin","admin")
resp, err := client.Do(req)
// handle err!
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
// handle err!
2021-11-23 16:26:50

Thanks for the reply. But what about the flags -u -s and -v how to include that
Pulkit Kundra

{"error":"parse \"-s http://<ip>/restconf/data/ -v\": first path segment in URL cannot contain colon","handler":"POST /collect","jobID":"Collect-11-24-2021_051320","level":"error","method":"initiateConfdCollection","msg":"Getrunningconfig: Failed to create request: parse \"-s http://<ip>/restconf/data/ -v\": first path segment in URL cannot contain colon","time":"2021-11-24T05:13:21Z"} {"handler":"POST /collect","jobID":"Collect-11-24-2021_051320","level":"error","method":"initiateConfdCollection","msg":"Failed to get data from Namespace: <ip>","time":"2021-11-24T05:13:21Z"}
Pulkit Kundra

Got the above error while using pkg.go.dev/net/http
Pulkit Kundra

If you want to go with the net/http approach and you are getting an error, best to paste that as a new question. Looks to me that have -s, the url, and -v all in the same string there.
Daniel Farrell

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................