Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go http查询字符串:问号变为%3_Go_Url - Fatal编程技术网

Go http查询字符串:问号变为%3

Go http查询字符串:问号变为%3,go,url,Go,Url,试图附加到现有查询字符串,但继续查询字符串的“?”将被替换为“%3” server := "server.com" endpoint:= "/buildWithParameters?token=<top_secret>" // concat server and build job u, err := url.Parse(server) u.Path = path.Join(u.Path, endpoint) endpoint := u.String(

试图附加到现有查询字符串,但继续查询字符串的“?”将被替换为“%3”

 server := "server.com"
 endpoint:= "/buildWithParameters?token=<top_secret>" 

// concat server and build job
    u, err := url.Parse(server)
    u.Path = path.Join(u.Path, endpoint)
    endpoint := u.String()

    // since we are appending to existing querystring, request is needed for parsing
    req, err := http.NewRequest("POST", endpoint, nil)
    if err != nil {
        return fmt.Errorf("Error creating post request: (%v)", err)
    }

    // read existing query string
    q := req.URL.Query()


    q.Add("key", "value")

    req.URL.RawQuery = q.Encode()


    log.Println(req.URL.String())
server:=“server.com”
端点:=“/buildWithParameters?令牌=”
//concat服务器和构建作业
u、 错误:=url.Parse(服务器)
u、 Path=Path.Join(u.Path,端点)
端点:=u.String()
//由于我们是对现有querystring进行追加,所以解析需要请求
req,err:=http.NewRequest(“POST”,端点,nil)
如果出错!=零{
返回fmt.Errorf(“创建post请求时出错:(%v)”,err)
}
//读取现有查询字符串
q:=req.URL.Query()
q、 添加(“键”、“值”)
req.URL.RawQuery=q.Encode()
log.Println(req.URL.String())
输出为:

https://server.com/job/jobbyjob/buildWithParameters%3Ftoken=?key=value

这是因为您正在将查询字符串附加到路径,因此库正在正确转义
以生成有效的URL。如果希望
token=
被视为查询的一部分,则需要将其添加到查询中:

endpoint:= "/buildWithParameters" 

// ... 

q.Add("token", "<top_secret>")
endpoint:=“/buildWithParameters”
// ... 
q、 添加(“令牌”,“”)

正如@Flimzy所指出的,是路径库导致了我的问题。已解析以下base.ResolveReference()方法:

// concat server and build job
u, err := url.Parse(server)
u.Path = path.Join(u.Path, endpoint)
endpoint := u.String()
替换为:

// concat server and build job
base, err := url.Parse(server)
endpoint, err := url.Parse(endpoint)
endpoint = base.ResolveReference(endpoint)

您使用
url.Parse
创建它,然后将查询字符串附加到路径(它通过转义正确处理该路径)。如果您采用另一种方式(构建完整的URL,然后对其进行解析),它可以完成以下工作:

server := "server.com"
endpoint := "/buildWithParameters?token=<top_secret>" 

// concat server and build job
u, err := url.Parse(path.Join(server, endpoint))
server:=“server.com”
端点:=“/buildWithParameters?令牌=”
//concat服务器和构建作业
u、 错误:=url.Parse(path.Join(服务器,端点))

谢谢,是的,路径库就是问题所在。我改用了.resolverence,效果非常好