Custom HTTP Headers with GWT RPC

Prior to GWT 2.0, there was no easy way to add custom HTTP headers when making remote procedure calls. The new RpcRequestBuilder in GWT 2.0 makes it easy to add custom headers.

// start with a custom RpcRequestBuilder
RpcRequestBuilder reqBuilder = new RpcRequestBuilder() {
  @Override
  protected RequestBuilder doCreate(String serviceEntryPoint) {
    RequestBuilder rb = super.doCreate(serviceEntryPoint);
    rb.setHeader("username", "sookie_stackhouse");
    return rb;
  }
};

// as with any other RPC, use GWT.create(...) to generate the client proxy
MyServiceAsync service = (MyServiceAsync) GWT.create(MyService.class);

// all client proxies also implement ServiceDefTarget
((ServiceDefTarget) service).setRpcRequestBuilder(reqBuilder);

// make calls as normal
service.doFunAndInterestingThings();

sensei Says:

Great work,
by the way , happy to see you started writing ;)
Best Regards

neato Says:

Great example on how to send custom headers from client to server, but how to read it back in on the client when server has changed the header?