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();

Leave a Reply

Your email address will not be published. Required fields are marked *