我有一个包含空格和特殊字符的 URL,如下例所示:
http://localhost:8182/graphs/graph/tp/gremlin?script=g.addVertex(['id':'0af69422 5be','date':'1968-01-16 00:00:00 +0000 UTC'])
Run Code Online (Sandbox Code Playgroud)
如何在 Go 中对这样的 URL 进行编码?
net/url包中提供了 URL 编码。您具有以下功能:
url.QueryEscape("g.addVertex(['id':'0af69422 5be','date':'1968-01-16 00:00:00 +0000 UTC'])")
Run Code Online (Sandbox Code Playgroud)
这给了你:
g.addVertex%28%5B%27id%27%3A%270af69422+5be%27%2C%27date%27%3A%271968-01-16+00%3A00%3A00+%2B0000+UTC%27%5D%29
另请查看url.URL和url.Values。根据您的需求,这些将为您解决。
如果您只有整个 URL,您可以尝试解析它,然后像这样重新编码损坏的查询部分:
u, err := url.Parse("http://localhost:8182/graphs/graph/tp/gremlin?script=g.addVertex(['id':'0af69422 5be','date':'1968-01-16 00:00:00 +0000 UTC'])")
if err != nil {
panic(err)
}
u.RawQuery = u.Query().Encode()
fmt.Println(u)
Run Code Online (Sandbox Code Playgroud)
但是,要小心。如果字符串包含和符号 (&) 或井号 (#),则不会给出您期望的结果。
| 归档时间: |
|
| 查看次数: |
2274 次 |
| 最近记录: |