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

Unexpected errors on empty HTTP request bodies #452

Closed
torkelrogstad opened this issue Feb 3, 2023 · 4 comments · Fixed by #459
Closed

Unexpected errors on empty HTTP request bodies #452

torkelrogstad opened this issue Feb 3, 2023 · 4 comments · Fixed by #459
Labels
enhancement New feature or request

Comments

@torkelrogstad
Copy link

When doing a HTTP request without a body I end up with this response:

{"code":"invalid_argument","message":"unmarshal into *emptypb.Empty: proto: syntax error (line 1:1): unexpected token "}   

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:

if r.ContentLength == 0 {
  r.Body = bytes.NewBuffer("{}")
}

// rest of the Connect magic
@torkelrogstad torkelrogstad added the enhancement New feature or request label Feb 3, 2023
@joshcarp
Copy link
Contributor

joshcarp commented Feb 6, 2023

I think that it's semantically correct to say that an empty string is not valid json: "" should always show as invalid with content-type: application/json.
Adding on {} to the body would also be adding json encoding specific logic to connect, which wouldn't be desired.

@torkelrogstad
Copy link
Author

torkelrogstad commented Feb 7, 2023

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 "cannot unmarshal MESSAGE_TYPE: empty request body", for example?

@akshayjshah
Copy link
Member

Improving the error message is definitely doable!

I'm particularly reluctant to implicitly treat "" and "{}" identically because it diverges from protojson, and is likely to introduce incompatibilities with other gRPC implementations. (This is a pretty soft objection, but I'd rather be conservative.)

@mattrobenolt
Copy link
Contributor

mattrobenolt commented Feb 9, 2023

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.

akshayjshah added a commit that referenced this issue Feb 9, 2023
Return a more understandable error when the JSON codec attempts to
unmarshal an empty body.

Fixes #452.
akshayjshah added a commit that referenced this issue Feb 14, 2023
Return a more understandable error when the JSON codec attempts to
unmarshal an empty body.

Fixes #452.
akshayjshah added a commit that referenced this issue Jul 26, 2023
Return a more understandable error when the JSON codec attempts to
unmarshal an empty body.

Fixes #452.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants