diff --git a/pkg/httpclient/http.go b/pkg/httpclient/http.go index 6be8743..42f4d11 100644 --- a/pkg/httpclient/http.go +++ b/pkg/httpclient/http.go @@ -7,8 +7,7 @@ import ( "fmt" "io" "net/http" - - "github.com/KyberNetwork/tradinglib/pkg/sb" + "net/url" ) func DoHTTPRequest(client *http.Client, req *http.Request, out interface{}, options ...Option) (*http.Response, error) { @@ -47,11 +46,25 @@ func NewRequest(method, baseURL, path string, query Query, body io.Reader) (*htt func NewRequestWithContext( ctx context.Context, method, baseURL, path string, query Query, body io.Reader, ) (*http.Request, error) { - url := baseURL + path + u, err := url.Parse(baseURL) + if err != nil { + return nil, fmt.Errorf("parse url: %w", err) + } + + if path != "" { + u = u.JoinPath(path) + } if query != nil { - url = sb.Concat(url, "?", query.String()) + existingQuery := u.Query() + for key, values := range existingQuery { + for _, value := range values { + query.AddString(key, value) + } + } + u.RawQuery = query.String() } - return http.NewRequestWithContext(ctx, method, url, body) + + return http.NewRequestWithContext(ctx, method, u.String(), body) } func NewGet(baseURL, path string, query Query) (*http.Request, error) { diff --git a/pkg/httpclient/query.go b/pkg/httpclient/query.go index 0429c46..d5000a1 100644 --- a/pkg/httpclient/query.go +++ b/pkg/httpclient/query.go @@ -37,6 +37,11 @@ func (q Query) SetString(key, value string) Query { return q } +func (q Query) AddString(key, value string) Query { + q.url().Add(key, value) + return q +} + func stringsContains(ss []string, token string) bool { for _, s := range ss { if s == token {