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

The gateway cannot handle the form parameters correctly #3771

Closed
gongluck opened this issue Dec 11, 2023 · 0 comments
Closed

The gateway cannot handle the form parameters correctly #3771

gongluck opened this issue Dec 11, 2023 · 0 comments

Comments

@gongluck
Copy link
Contributor

Gateway configuration parameters:

Name: demo-gateway
Host: localhost
Port: 8889
Log:
  Level: debug
Upstreams:
  - Grpc:
      Target:
        127.0.0.1:8081
    ProtoSets:
      - hello.pb
    Mappings:
      - Method: post
        Path: /ping
        RpcPath: hello.Hello/Ping

Test with postman:

1}MDW}GP)($NV)T32$AYK
4{ ZFK$AB62GPRDYGGH_~_F
`BW5QAA55IRV2$RY8 5TLLS

Cause of the problem

// NewRequestParser creates a new request parser from the given http.Request and resolver.
func NewRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.RequestParser, error) {
	vars := pathvar.Vars(r)
	params, err := httpx.GetFormValues(r)
	if err != nil {
		return nil, err
	}

	for k, v := range vars {
		params[k] = v
	}

	body, ok := getBody(r)
	if !ok {
		return buildJsonRequestParser(params, resolver)
	}

	if len(params) == 0 {
		return grpcurl.NewJSONRequestParser(body, resolver), nil
	}

	m := make(map[string]any)
	if err := json.NewDecoder(body).Decode(&m); err != nil {
                // here return io.EOF
                // Because the body's data has already been read by the above call to function GetFormValues?
                // Even though there's an error, but params maybe have data!
		return nil, err
	}

	for k, v := range params {
		m[k] = v
	}

	return buildJsonRequestParser(m, resolver)
}

My solution

// NewRequestParser creates a new request parser from the given http.Request and resolver.
func NewRequestParser(r *http.Request, resolver jsonpb.AnyResolver) (grpcurl.RequestParser, error) {
	vars := pathvar.Vars(r)
	params, err := httpx.GetFormValues(r)
	if err != nil {
		return nil, err
	}

	for k, v := range vars {
		params[k] = v
	}

	body, ok := getBody(r)
	if !ok {
		return buildJsonRequestParser(params, resolver)
	}

	if len(params) == 0 {
		return grpcurl.NewJSONRequestParser(body, resolver), nil
	}

	m := make(map[string]any)
	if err := json.NewDecoder(body).Decode(&m); err != nil && err != io.EOF { // skip eof
		return nil, err
	}

	for k, v := range params {
		m[k] = v
	}

	return buildJsonRequestParser(m, resolver)
}

QX{{8S9BE(SK5RPC ZD6NC7
45VPIHA2)H{H0@`QE@JO9E3

End

If my changes are accepted, I can initiate a pr, or do you have a better way?

@gongluck gongluck reopened this Dec 12, 2023
gongluck added a commit to gongluck/go-zero that referenced this issue Dec 13, 2023
kevwan pushed a commit to gongluck/go-zero that referenced this issue Dec 16, 2023
github-merge-queue bot pushed a commit that referenced this issue Dec 16, 2023
WqyJh pushed a commit to WqyJh/go-zero that referenced this issue Dec 21, 2023
dongmeng199 pushed a commit to dongmeng199/go-zero that referenced this issue Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant