Skip to content

Commit

Permalink
fix: add content-type header to post requests and don't throw error i…
Browse files Browse the repository at this point in the history
…f a status created is returned
  • Loading branch information
antonmoller committed Jun 20, 2023
1 parent adaae51 commit e65955d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,22 @@ func (c *clientImpl) getToken(ctx context.Context) (_ *Token, err error) {

// Template method to execute GET requests towards monta.
func doGet[T any](ctx context.Context, client *clientImpl, path string, query url.Values) (*T, error) {
return execute[T](ctx, client, http.MethodGet, path, query, nil)
return execute[T](ctx, client, http.MethodGet, path, query, nil, nil)
}

// Template method to execute POST requests towards monta.
func doPost[T any](ctx context.Context, client *clientImpl, path string, body io.Reader) (*T, error) {
return execute[T](ctx, client, http.MethodPost, path, nil, body)
return execute[T](ctx, client, http.MethodPost, path, nil, &http.Header{"content-type": {"application/json"}}, body)
}

// Template method to execute PATCH requests towards monta.
func doPatch[T any](ctx context.Context, client *clientImpl, path string, body io.Reader) (*T, error) {
return execute[T](ctx, client, http.MethodPatch, path, nil, body)
return execute[T](ctx, client, http.MethodPatch, path, nil, nil, body)
}

// Template method to execute DELETE requests towards monta.
func doDelete(ctx context.Context, client *clientImpl, path string) error {
_, err := execute[any](ctx, client, http.MethodDelete, path, nil, nil)
_, err := execute[any](ctx, client, http.MethodDelete, path, nil, nil, nil)
return err
}

Expand All @@ -184,6 +184,7 @@ func execute[T any](
method string,
path string,
query url.Values,
headers *http.Header,
body io.Reader,
) (_ *T, err error) {
defer func() {
Expand All @@ -202,6 +203,9 @@ func execute[T any](
if err != nil {
return nil, err
}
if headers != nil {
httpRequest.Header = *headers
}
if err := client.setAuthorization(ctx, httpRequest); err != nil {
return nil, err
}
Expand All @@ -212,7 +216,7 @@ func execute[T any](
defer func() {
_ = httpResponse.Body.Close()
}()
if httpResponse.StatusCode != http.StatusOK {
if httpResponse.StatusCode != http.StatusOK && httpResponse.StatusCode != http.StatusCreated {
return nil, newStatusError(httpResponse)
}
respBody, err := ioutil.ReadAll(httpResponse.Body)
Expand Down

0 comments on commit e65955d

Please sign in to comment.