Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always add Content-Type header for non GET APIs #148

Merged
merged 5 commits into from
Nov 14, 2023
Merged

Conversation

yohfee
Copy link
Contributor

@yohfee yohfee commented Nov 13, 2023

It seems that non GET APIs must always have Content-Type: application/json request header even though they don't require request body.

For example,

% curl https://api.mackerelio.com/api/v0/aws-integrations-external-id -X POST -H "X-Api-Key: ..."
{"error":{"message":"Illegal Content-Type. Please try with \"Content-Type: application/json\""}}

% curl https://api.mackerelio.com/api/v0/channels/XXX -X DELETE -H "X-Api-Key: ..."
{"error":{"message":"Illegal Content-Type. Please try with \"Content-Type: application/json\""}}

But recent changes stop sending them and got errors like error API request failed: Illegal Content-Type. Please try with "Content-Type: application/json".

@Arthur1 Arthur1 self-requested a review November 13, 2023 06:46
mackerel.go Outdated
@@ -174,7 +174,7 @@ func requestInternal[T any](client *Client, method, path string, params url.Valu
if err != nil {
return nil, nil, err
}
if body != nil {
if body != nil || method != "GET" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use http.MethodGet constant instead of "GET" will make the implementation more safety.

https://go.dev/src/net/http/method.go

Comment on lines +54 to +62
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if !test.hasContentTypeHeader && req.Header.Get("Content-Type") == "application/json" {
t.Error("Content-Type header should not have application/json")
}
if test.hasContentTypeHeader && req.Header.Get("Content-Type") != "application/json" {
t.Error("Content-Type header should have application/json")
}
res.Write([]byte(`{"success": true}`)) // nolint
}))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a comment that this test server's behavior is similar to Mackerel's public API?

@itchyny
Copy link
Contributor

itchyny commented Nov 14, 2023

Oops, I didn't notice that Content-Type is required on sending empty body. Sorry about that.

@yohfee yohfee merged commit 2b4e0de into master Nov 14, 2023
10 checks passed
@yohfee yohfee deleted the fix_request_header branch November 15, 2023 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants