Fix internal HTTP API calls
@@ -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)
}
