I am using gRPC gateway for the backward compatibility support but I encountered an issue where in our REST APIs we are using the Protobuf with REST. But in gRPC gateway we are using JSON as a response type. Now when we have migrated towards gRPC, but I am not seeing any option where I will receive the response as a proto type not the JSON type. Can someone please help me in this?
I am getting content-type with the REST url: application/json
What I need: application/x-protobuf
EDIT: I have this service contract
service TestService {
rpc GetBattleStatusByEntityIds(ProtoRequest) returns (ProtoResponse) {
option (google.api.http) = {
get : "/test"
};
}
}
When I am hitting this /test url then I am receiving the response with content type application/json but I need the data in application/x-protobuf format. I checked the grpc-gateway code and it is using a marshaller which change the response from protobuf to json. Can you please help me to keep it like json any code sample would help.
https://github.com/grpc-ecosystem/grpc-gateway/blob/main/runtime/marshal_jsonpb.go
Picture of wanted response type: https://i.stack.imgur.com/jvR3w.png
You need to add an
Acceptheader - which grpc-gateway will then use when deciding how to render the response.In the most simple sense,
Content-Typeindicates to the server what type of content the request body is, whilstAccepttells the server what type of content you're willing to accept in the response body.So in your case, you'll want to use
Accept: application/x-protobufContinue to set the
Content-Typeheader if you're sending content too.