Yonoss
Yonoss

Reputation: 1688

How to get the request headers from a grpc request in java?

I'm building a Java grpc server and I'm having a hard time getting the request headers. The proto files are compiled using protobuf-maven-plugin, and based on the generated stubs, I can't access the context or the request metadata.

I've also tried to define a list of key/values in the message request, hoping the grpc will take care of the headers mappings, but no luck so far.

Any idea how I can get access the headers?

Thanks!

My proto files content:

...
// Version Request
message VersionRequest {
  // key/value pairs
  repeated Header headers = 1;
}

message Header {
  //key
  string key = 1;
  //value
  string constant = 2;
}
...



   ...
security_definitions: {
    security: {
      key: "clientIdAuth";
      value: {
        type: TYPE_API_KEY;
        in: IN_HEADER;
        name: "x-client-id";
      }
    }
    security: {
      key: "clientSecretAuth";
      value: {
        type: TYPE_API_KEY;
        in: IN_HEADER;
        name: "x-client-secret";
      }
    }
    security: {
      key: "bearerAuth";
      value: {
        type: TYPE_API_KEY;
        in: IN_HEADER;
        name: "Authorization";
      }
    }
  }
    ...
    // Retrieves system version
      //
      // Retrieves system version
      rpc GetVersion(VersionRequest) returns (Version) {
        option (google.api.http) = {
          get : "/api/v4/version"
        };
        option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = {
          security: {
            security_requirement: {
              key: "clientIdAuth";
              value: {}
            }
            security_requirement: {
              key: "clientSecretAuth";
              value: {}
            }
            security_requirement: {
              key: "bearerAuth";
              value: {}
            }
          }
        };
      }
    ...

The server side implementation looks like this:

...
@GrpcService
public class GrpcAPIService extends MixAPIGrpc.MixAPIImplBase {

    @Autowired
    private MwProxy mwProxy;

    @Override
    public void getVersion(VersionRequest versionRequest, StreamObserver<Version> streamObserver) {
        System.out.println("===============" + versionRequest.getHeadersCount());
        handleEGrpcCall("getVersion", mwProxy::getVersion, streamObserver);
    }
...

versionRequest.getHeadersCount() always returns 0, although there are 3 headers sent in all my requests.

Upvotes: 2

Views: 19547

Answers (2)

Adahuangsx
Adahuangsx

Reputation: 11

The headers, which I suppose is the HTTP headers you are talking about, belongs to the HTTP protocol. That is why you failed to get it in the gRPC layer. Make sure to retrieve it in Servlet, or Controller if you use MVC framework (add HttpServletRequest request param as below).

  @GetMapping("hello")
  public CompletableFuture<GreetingResponse> greet(HttpServletRequest request, @RequestParams someParam) {
  // call gRPC Service here
}

Then you have access to the headers by request.getHeader("User-Agent")

Upvotes: 1

user675693
user675693

Reputation: 335

Unless you are using a custom grpc-java codegen plugin provided by a third party, the standard grpc codegen plugin shipped with grpc-java library https://search.maven.org/search?q=a:protoc-gen-grpc-java%20g:io.grpc does not support custom method options such as

option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation)

So you can not take advantage of that options. What grpc-java can do is to send and receive io.grpc.Metadata via client and server interceptors. See examples: https://github.com/grpc/grpc-java/tree/v1.33.1/examples/src/main/java/io/grpc/examples/header

Upvotes: 1

Related Questions