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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime.WithForwardResponseOption() is not respected #1625

Closed
labsvisual opened this issue Aug 28, 2020 · 3 comments
Closed

runtime.WithForwardResponseOption() is not respected #1625

labsvisual opened this issue Aug 28, 2020 · 3 comments

Comments

@labsvisual
Copy link

馃悰 Bug Report

When forward response options are provided to the runtime, they are not executed.

To Reproduce

mux := runtime.NewServeMux(
	runtime.WithForwardResponseOption(httpResponseModifier),
)

Here, httpResponseModifier has the following definition (this is a direct copy of the related documentation):

func httpResponseModifier(ctx context.Context, w http.ResponseWriter, p protoiface.MessageV1) error {
        fmt.Println("in response modifier") // debug:withContext(debugger::parent)

	md, ok := runtime.ServerMetadataFromContext(ctx)
	if !ok {
		return nil
	}

	// set http status code
	if vals := md.HeaderMD.Get("x-http-code"); len(vals) > 0 {
		code, err := strconv.Atoi(vals[0])
		if err != nil {
			return err
		}
		w.WriteHeader(code)

		// delete the headers to not expose any grpc-metadata in http response
		delete(md.HeaderMD, "x-http-code")
		delete(w.Header(), "Grpc-Metadata-X-Http-Code")
	}

	return nil
}

The piece in the documentation uses proto.Message but that throws an error:
Screen Shot 2020-08-28 at 8 24 35 PM

Expected behavior

The forward response options should be executed.

Actual Behavior

The httpResponseModifier function does not execute. I tried adding code to the actual package to help me with debugging and mux.forwardResponseOptions does have a len() > 1 so I am not sure what's happening here.

Your Environment

macOS 10.15.5 (19F101)
go version go1.14.7 darwin/amd64
grpc-gateway 1.14.7

@johanbrandhorst
Copy link
Collaborator

Hi Shreyansh, this is likely happening because you're using the APIv2 protobuf stack. Please try again with grpc-gateway v2: https://github.com/grpc-ecosystem/grpc-gateway/releases/tag/v2.0.0-beta.4. See https://github.com/grpc-ecosystem/grpc-gateway/blob/v2/docs/_docs/v2-migration.md for the migration guide.

@labsvisual
Copy link
Author

labsvisual commented Aug 28, 2020

Just updated the stack to v2, the error (in the image) went but the forwardResponseOptions are still not being executed. The code is absolutely the same (save the import as "github.com/grpc-ecosystem/grpc-gateway/v2/runtime".)

@johanbrandhorst

@johanbrandhorst
Copy link
Collaborator

This would be easier to help with on our Slack support channel, #grpc-gateway on Gophers Slack. This isn't the best forum for this sort of debugging. Could we move this conversation there?

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

2 participants