0
0
Fork 0
mirror of https://github.com/matrix-org/dendrite synced 2024-12-13 15:53:16 +01:00

Fix internal HTTP API calls

This commit is contained in:
Neil Alexander 2020-05-22 13:54:04 +01:00
parent 3daa2327ed
commit 3d06fe91f2

View file

@ -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)
}