如何在grpc-gateway中执行302重定向

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.

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.

https://grpc-ecosystem.github.io/grpc-gateway/docs/customizingyourgateway.html#replace-a-response-forwarder-per-method

Looking at the code you mentioned, it appears to be simply mapping the grpc status codes directly to their closest http equivalents. It doesn't appear that there are any codes in the spec that really map to an http redirect. Am I correct in assuming you are using the gateway to connect a browser to a grpc service?

My suggestion would be to work redirects into the protocol somehow. What if the response to certain methods was something like:

message HelloResponse {
  string reply = 1;
  bool shouldRedirect = 2;
  string redirectURL = 3;
}

Then if the receiver could detect that from the response and redirect client-side. A little less magical, but still lets you do the redirect if needed.