gogrpc-gateway

How to do a 302 redirect in grpc-gateway


I use grpc-gateway to host an HTTP server out of my proto definitions. It works great overall.

However, for one special endpoint, instead of returning a value back, I want to do a 302 redirect to an image hosted in an s3.

If you want to return an error via grpc-gateway, you can return it like

nil, status.Error(codes.Unauthenticated, "Nope")

I wonder if there is something similar to do a 302 redirect?

As far as I get from this page it seems unlikely. I hope I overlooked something.


Solution

  • There's no straightforward way. But there's a workaround.

    There's no conception in gRPC similar to 302. So simple error code mappings won't work fine. But you can overwrite a response forwarder per method so that it extracts redirectURL from the response and sets the HTTP status code and Location header.

    Documentation link: https://grpc-ecosystem.github.io/grpc-gateway/docs/mapping/customizing_your_gateway/#mutate-response-messages-or-set-response-headers