How to get headers in httpservice with thrift protocol

419 Views Asked by At
HelloWorldService.Iface helloService =
        Clients.builder("tbinary+http://127.0.0.1:8080/hello")
               .addHttpHeader("key", "value")
               .build(HelloWorldService.Iface.class);

ServerBuilder sb = Server.builder();
sb.service("/hello", THttpService.of(new HelloServiceImpl()));
sb.http(8080);
Server server = sb.build();
server.start();

How do I handle HTTP headers in the server? THttpService is a final class, so I can not extend it to handle the headers.

1

There are 1 best solutions below

0
On

In your HelloServiceImpl class, you can access the current ServiceRequestContext using ServiceRequestContext.current() to access the request headers and other information:

public class HelloServiceImpl implements HelloWorldService.AsyncIface {
    @Override
    public void hello(...) {
        ServiceRequestContext ctx = ServiceRequestContext.current();
        RequestHeaders headers = ctx.request().headers();
        String value = headers.get("key");
        ...
    }
}