grpc-ecosystem / grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec

Home Page:https://grpc-ecosystem.github.io/grpc-gateway/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request body not parsed when using X-HTTP-Method-Override header

netrounds-guillaume opened this issue Β· comments

πŸ› Bug Report

There is a bug around this code:

grpc-gateway/runtime/mux.go

Lines 324 to 330 in 1c444cc

r.Method = strings.ToUpper(override)
if err := r.ParseForm(); err != nil {
_, outboundMarshaler := MarshalerForRequest(s, r)
sterr := status.Error(codes.InvalidArgument, err.Error())
s.errorHandler(ctx, s, outboundMarshaler, w, r, sterr)
return
}

r.Method is overridden to GET before r.ParseForm() which causes ParseForm to skip parsing the request body and goes straight to parsing from the query string parameters. Since the whole point of using X-HTTP-Method-Override is to move the query string parameters to the request body, no query string parameters are present in the URL

One possible solution is to move r.Method = strings.ToUpper(override) after the r.ParseForm() block.

To Reproduce

Send a POST request to a GET endpoint with Content-Type: application/x-www-form-urlencoded and X-HTTP-Method-Override: GET headers. Set the request body with what would be query string parameters e.g. limit=15&page=1

Expected behavior

Form parameters are parsed and passed along to the API handler

Actual Behavior

Form parameters are not parsed and none of the parameters are populated when reaching the API handler

Your Environment

(no info)

Hi, thanks for the detailed issue! The problem as you describe it seems indeed to be that we're mutating the request before parsing. Would you be willing to contribute a PR with a fix and a test confirming the fix?