How to set HTTP header in RESTEasy client framework?

RESTEasy (a JAX-RS implementation) has a nice client framework, eg:


SimpleClient client = ProxyFactory.create(SimpleClient.class, "http://localhost:8081");
client.putBasic("hello world");

How do you set HTTP headers?


The solution proposed by jkeeler is a good approach, but I want to set HTTP headers on ProxyFactory level and I don't want to pass headers to the client object. Any ideas?


With RestEasy 3.x I use ClientRequestFilters. In the below example there is a continuous integration (CI) server listening for requests running in the background. The test and the CI server use the same database and entity classes.

Assume that a tenant named 'test-tenant' does in fact exist, and there is a user 'root' that belongs to that tenant, and the user has the password specified below.

private static final String BASE_URI = "http://localhost:" + PORT;
@Test(groups = "functionalTests")
public void testGetTenant() throws Exception {
    Client client = ClientBuilder.newClient();
    ResteasyWebTarget target = (ResteasyWebTarget);
    client.register(new AddAuthHeadersRequestFilter("root", "DefaultPasswordsAre:-("));
    TenantResource resource = target.proxy(TenantResource.class);

    RestTenant restTenant = resource.getTenant(;

    assertThat(restTenant.getName(), is("test-tenant"));
    assertThat(restTenant.isActive(), is(true));

And the AddAuthHeadersRequestFilter class:

public static class AddAuthHeadersRequestFilter implements ClientRequestFilter {

    private final String username;
    private final String password;

    public AddAuthHeadersRequestFilter(String username, String password) {
        this.username = username;
        this.password = password;

    public void filter(ClientRequestContext requestContext) throws IOException {
        String token = username + ":" + password;
        String base64Token = Base64.encodeBase64String(token.getBytes(StandardCharsets.UTF_8));
        requestContext.getHeaders().add("Authorization", "Basic " + base64Token);

The import statements (assuming you just paste the test and the static class into a single TestNg test-class file):

import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.testng.annotations.Test;
import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.binary.Base64;

In your client proxy interface, use the @HeaderParam annotation:

public interface SimpleClient
   public void putBasic(@HeaderParam("Greeting") String greeting);

The call in your example above would add an HTTP header that looks like this:

Greeting: hello world

I have found a solution:

import org.apache.commons.httpclient.HttpClient;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClientExecutor;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

HttpClient httpClient = new HttpClient();
ApacheHttpClientExecutor executor = new ApacheHttpClientExecutor(httpClient) {
    public ClientResponse execute(ClientRequest request) throws Exception {
        request.header("X-My-Header", "value");
        return super.execute(request);

SimpleClient client = ProxyFactory.create(SimpleClient.class, "http://localhost:8081", executor);
client.putBasic("hello world");

Even easier:

    ResteasyClient client = new ResteasyClientBuilder().build();
    ResteasyWebTarget target ="");
    Response response = target.request().header("Authorization", "Basic test123")
            .acceptEncoding("gzip, deflate")
            .post(Entity.entity(some_xml, "application/x-www-form-urlencoded"));

Need Your Help

How to "hide" not "exclude" quick filter selection

filter set tableau

I need help for selection in quick filters.

what can i use to create a revision tree graphic on linux

linux svn tree graph revision

Which tool should I use in order to create a revision tree for a whole svn repository on linux. It's going to be like a visualization of every action made on that repository.