Skip to content

Commit 4f16b7d

Browse files
authored
Merge pull request #31 from tri-adam/issue-30
Fix BaseURL Path Handling
2 parents 3f4fe94 + fdc5581 commit 4f16b7d

File tree

3 files changed

+423
-228
lines changed

3 files changed

+423
-228
lines changed

client/client.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"net/http"
1212
"net/url"
13+
"strings"
1314

1415
"github.com/go-log/log"
1516
)
@@ -66,6 +67,13 @@ func New(cfg *Config) (c *Client, err error) {
6667
return nil, fmt.Errorf("unsupported protocol scheme %q", baseURL.Scheme)
6768
}
6869

70+
// If baseURL has a path component, ensure it is terminated with a separator, to prevent
71+
// url.ResolveReference from stripping the final component of the path when constructing
72+
// request URL.
73+
if p := baseURL.Path; p != "" && !strings.HasSuffix(p, "/") {
74+
baseURL.Path += "/"
75+
}
76+
6977
c = &Client{
7078
BaseURL: baseURL,
7179
AuthToken: cfg.AuthToken,
@@ -87,10 +95,10 @@ func New(cfg *Config) (c *Client, err error) {
8795
return c, nil
8896
}
8997

90-
// newRequest returns a new Request given a method, path, query, and optional body.
98+
// newRequest returns a new Request given a method, relative path, query, and optional body.
9199
func (c *Client) newRequest(method, path string, body io.Reader) (r *http.Request, err error) {
92100
u := c.BaseURL.ResolveReference(&url.URL{
93-
Path: path,
101+
Path: strings.TrimPrefix(path, "/"), // trim leading separator as path is relative.
94102
})
95103

96104
r, err = http.NewRequest(method, u.String(), body)

0 commit comments

Comments
 (0)