-
Notifications
You must be signed in to change notification settings - Fork 33
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
Conversation
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" { |
There was a problem hiding this comment.
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.
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 | ||
})) |
There was a problem hiding this comment.
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?
Oops, I didn't notice that Content-Type is required on sending empty body. Sorry about that. |
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,
But recent changes stop sending them and got errors like
error API request failed: Illegal Content-Type. Please try with "Content-Type: application/json"
.