-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Encoding with application/x-www-form-urlencoded #7
Comments
Is this supported or still open? I'm implementing an OAuth2 server and the RFC requires the request Content-Type to be application/x-www-form-urlencoded. |
we ended up rewriting
|
+1 It would be useful to have it built-in! |
After one afternoon reading the f**king source code. This solution is not good.
The good solution is:
|
* add generation second file with private methods * update logic * update README.md * remove private definitions
any update? @yugui |
I don't know anyone working on this at the moment. Are you interested in helping add support for this? |
@johanbrandhorst I'll try it after reading the relevant code |
what are the protocols for this in 2022? We are trying to support receiving data from third party APIs (so we cannot control the format of the post request) and we ran into this issue. What are people doing to properly serve x-www-form-urlencoded via gprc-gateway? Is there any option besides writing our own custom marshaler? I see this pull request but it was not merged, and it references another PR that was merged but it doesn't seem to affect whether or not we can add support for x-www-form-urlencoded requests |
I think your only recourse today is a custom marshaler. |
Below is my custom marshaler for decoding the request in x-www-form-urleencoded format and returning the response in JSON. Hope this can help someone who suffer the same problem. package marshaler
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/grpc-ecosystem/grpc-gateway/v2/utilities"
"google.golang.org/protobuf/proto"
)
type UrlEncodeMarshal struct {
runtime.Marshaler
}
// ContentType means the content type of the response
func (u UrlEncodeMarshal) ContentType(_ interface{}) string {
return "application/json"
}
func (u UrlEncodeMarshal) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// NewDecoder indicates how to decode the request
func (u UrlEncodeMarshal) NewDecoder(r io.Reader) runtime.Decoder {
return runtime.DecoderFunc(func(p interface{}) error {
msg, ok := p.(proto.Message)
if !ok {
return fmt.Errorf("not proto message")
}
formData, err := ioutil.ReadAll(r)
if err != nil {
return err
}
values, err := url.ParseQuery(string(formData))
if err != nil {
return err
}
filter := &utilities.DoubleArray{}
err = runtime.PopulateQueryParameters(msg, values, filter)
if err != nil {
return err
}
return nil
})
} |
This works fine. Any chance this could get merge in here? I'm happy to contribute in unit testing as it seems that was missing part in https://github.com/grpc-ecosystem/grpc-gateway/pull/548/files |
Yes, we'd be happy to accept a new contribution for this, thank you! |
@jhowliu do you prefer to make a PR yourself so I add the tests to your PR or should I create the PR with your code myself? |
Hi @mickael-kerjean , sorry about waiting for a while. I have made a PR. Feel free to add the tests. https://github.com/jhowliu/grpc-gateway/tree/feat/support-url-form-encoded-marshaler |
Today request/response body is always encoded in JSON.
Should accept application/x-www-form-urlencoded too.
The text was updated successfully, but these errors were encountered: