-
Notifications
You must be signed in to change notification settings - Fork 100
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
Unexpected errors on empty HTTP request bodies #452
Comments
I think that it's semantically correct to say that an empty string is not valid json: |
In that case I think the error message should be improved. It took me quite a bit of head-scratching to understand what this meant. Something along the lines of |
Improving the error message is definitely doable! I'm particularly reluctant to implicitly treat |
imo this is pretty easy to solve in your own tiny bit of middleware. I agree, it's technically invalid and incorrect. If someone using connect-go wants to deviate, they have the tools to do so. Something like: func EnsureBody(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.ContentLength == 0 && r.Headers.Get("Content-Type") == "application/json" {
r.Body = bytes.NewBuffer("{}")
r.ContentLength = 2
}
next.ServeHTTP(w, r)
})
}
var mux http.Handler = http.NewServeMux()
mux = EnsureBody(mux) something like that, entirely untested, etc, but this is simple enough to implement your logic for your use case. |
Return a more understandable error when the JSON codec attempts to unmarshal an empty body. Fixes #452.
Return a more understandable error when the JSON codec attempts to unmarshal an empty body. Fixes #452.
Return a more understandable error when the JSON codec attempts to unmarshal an empty body. Fixes #452.
When doing a HTTP request without a body I end up with this response:
This is fixed by adding an empty body
{}
to my HTTP request, so it's not a major issue. However, I'd expect empty bodies to result in the empty value for my requests. Is that a strange expectation?Basically, I want this to happen on the server side before my request handler is invoked, in Go-pseudo:
The text was updated successfully, but these errors were encountered: