From 3d06fe91f278413bf883d853575239e322402b2e Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 22 May 2020 13:54:04 +0100 Subject: [PATCH] Fix internal HTTP API calls --- internal/http/http.go | 16 +++++++++++++--- 1 file 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) }