Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape space characters in golang GET request

Tags:

http

go

I want to send a text string in get request using net/http package in golang. But I'm unable to find out how to do that. Like I want to hit the following url:

"http://api.example.com/tutor?message=how can I do this"

Please let me know how can I do this.

like image 533
Shailesh Avatar asked Jan 07 '23 15:01

Shailesh


1 Answers

Use this:

resp, err := http.Get("http://api.example.com/tutor?message=" + url.QueryEscape("how can I do this"))

Where url the net/url package.

If you have multiple query parameters, you can use

p := url.Values{"message": {"hown ca I do this"}, "other": "whatever"}
resp, err := http.Get(`http://api.example.com/tutor?` + p.Encode())
like image 86
Bayta Darell Avatar answered Jan 16 '23 04:01

Bayta Darell