如何在Golang的GET请求中转义空格字符

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

How to escape space characters in golang GET request

问题

我想在golang的net/http包中使用GET请求发送一个文本字符串,但是我不知道如何做到这一点。比如我想要访问以下URL:

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

请告诉我如何实现这个。

英文:

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

得分: 10

使用以下代码:

resp, err := http.Get("http://api.example.com/tutor?message=" + url.QueryEscape("我该如何做这个"))

其中 urlnet/url 包

如果你有多个查询参数,可以使用以下代码:

p := url.Values{"message": {"我该如何做这个"}, "other": "whatever"}
resp, err := http.Get(`http://api.example.com/tutor?` + p.Encode())
英文:

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

huangapple
  • 本文由 发表于 2015年9月2日 06:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/32341589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定