fix: only encode when have params

v1.1.2
git 2026-03-13 01:29:20 +08:00
parent bec742852a
commit 2e095de7f6
Signed by: git
GPG Key ID: 3F65EFFA44207ADD
1 changed files with 20 additions and 12 deletions

View File

@ -185,8 +185,6 @@ func (c *Client) chain() http.RoundTripper {
// mergeURL return the merged url with common header and params // mergeURL return the merged url with common header and params
func (c *Client) mergeURL(u *url.URL) (*url.URL, error) { func (c *Client) mergeURL(u *url.URL) (*url.URL, error) {
query := make(url.Values)
if c.baseURL != nil { if c.baseURL != nil {
if u.Host != "" || u.Scheme != "" { if u.Host != "" || u.Scheme != "" {
return nil, fmt.Errorf("baseURL and request url host/schema can not be set at the same time") return nil, fmt.Errorf("baseURL and request url host/schema can not be set at the same time")
@ -195,13 +193,21 @@ func (c *Client) mergeURL(u *url.URL) (*url.URL, error) {
u.Scheme = c.baseURL.Scheme u.Scheme = c.baseURL.Scheme
u.Host = c.baseURL.Host u.Host = c.baseURL.Host
u.Path = path.Join(c.baseURL.Path, u.Path) u.Path = path.Join(c.baseURL.Path, u.Path)
maps.Copy(query, c.baseURL.Query())
} }
// change the url query // 收集 baseURL 和 commonQueryParams 中的额外查询参数,
maps.Copy(query, c.commonQueryParams) // 仅当存在额外参数时才合并并重编码,保留原始 RawQuery 中的特殊字符不被误编码
maps.Copy(query, u.Query()) extra := make(url.Values)
u.RawQuery = query.Encode() if c.baseURL != nil {
maps.Copy(extra, c.baseURL.Query())
}
maps.Copy(extra, c.commonQueryParams)
if len(extra) > 0 {
query := u.Query()
maps.Copy(query, extra)
u.RawQuery = query.Encode()
}
return u, nil return u, nil
} }
@ -383,11 +389,13 @@ func (r *Request[T]) getURL(u string) (string, error) {
return "", err return "", err
} }
// change the url query // 仅当有额外查询参数时才重编码,避免 url.Values.Encode() 将原始 RawQuery 中
query := newURL.Query() // 合法的特殊字符(如 CDN 签名令牌里的 '=')误编码为 %3D 导致服务端拒绝请求
maps.Copy(query, r.queryParams) if len(r.queryParams) > 0 {
query := newURL.Query()
newURL.RawQuery = query.Encode() maps.Copy(query, r.queryParams)
newURL.RawQuery = query.Encode()
}
return newURL.String(), nil return newURL.String(), nil
} }