diff options
author | Neil Alexander <neilalexander@users.noreply.github.com> | 2020-05-22 13:54:04 +0100 |
---|---|---|
committer | Neil Alexander <neilalexander@users.noreply.github.com> | 2020-05-22 13:54:04 +0100 |
commit | 3d06fe91f278413bf883d853575239e322402b2e (patch) | |
tree | 7d181a621baf15c3ab2da1c831183e27887dabae /internal | |
parent | 3daa2327edc90e1ff70bcbc578b793ae5ba5b69f (diff) |
Fix internal HTTP API calls
Diffstat (limited to 'internal')
-rw-r--r-- | internal/http/http.go | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/internal/http/http.go b/internal/http/http.go index 3c647544..d0b4d6c5 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -6,6 +6,8 @@ import ( "encoding/json" "fmt" "net/http" + "net/url" + "strings" opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" @@ -21,6 +23,14 @@ func PostJSON( return err } + parsedAPIURL, err := url.Parse(apiURL) + if err != nil { + return err + } + + parsedAPIURL.Path = "/api/" + strings.TrimLeft(parsedAPIURL.Path, "/") + apiURL = parsedAPIURL.String() + req, err := http.NewRequest(http.MethodPost, apiURL, bytes.NewReader(jsonBytes)) if err != nil { return err @@ -48,10 +58,10 @@ func PostJSON( var errorBody struct { Message string `json:"message"` } - if err = json.NewDecoder(res.Body).Decode(&errorBody); err != nil { - return err + if msgerr := json.NewDecoder(res.Body).Decode(&errorBody); msgerr == nil { + return fmt.Errorf("api: %d from %s: %s", res.StatusCode, apiURL, errorBody.Message) } - return fmt.Errorf("api: %d: %s", res.StatusCode, errorBody.Message) + return fmt.Errorf("api: %d from %s", res.StatusCode, apiURL) } return json.NewDecoder(res.Body).Decode(response) } |