How to escape space characters in golang GET request

时间:2015-09-01 22:02:35

标签: 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.

1 个答案:

答案 0 :(得分:8)

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())