在Golang中包含大括号的URL的错误请求400

时间:2018-04-27 16:37:12

标签: url go curly-braces

来自Python

import requests    
requests.get('https://git.eclipse.org/r/changes/?q=since:{2018-01-01 00:00:00.000}+AND+until:{2018-01-01 22:59:59.999}')

就像一个魅力。

在Go中,

client := &http.Client{Timeout: time.Second * 10}
response, err := client.Get("https://git.eclipse.org/r/changes/?q=since:{2018-01-01 00:00:00.000}+AND+until:{2018-01-01 22:59:59.999}")

导致错误请求(400)。

我假设问题是URL中大括号的编码。我该如何解决?

1 个答案:

答案 0 :(得分:6)

您需要转义查询字符串:

 client.Get("https://git.eclipse.org/r/changes/?q=" + url.QueryEscape("since:{2018-01-01 00:00:00.000}+AND+until:{2018-01-01 22:59:59.999}"))

您可能需要将+更改回空格,因为它们正在转义。