Send HTTP Request

When testing RESTful Web Services, you can’t just enter a URL in browser since that can only send a GET request. In order to conduct other request types, one way you can do is to use a ClientBuilder.

There are 3 steps to send a request using ClientBuilder:
  1. Create an Entity; (
  2. Configure an Invocation$Builder;
  3. Use Invocation$Builder to send a request attached with the Entity.


Because ClientBuilder itself is defined in, it needs a JavaEE container, or at least, the RESTEasy JAX-RS API, which I like to recommend.

Step 1. Create an Entity

The Entity creating here is mainly considered as a JSON or XML object wrapped by the Entity class, although you have many options. The following code shows how an Entity is conducted:

// Conduct an entity using content, which is an JSON String
Entity<String> entity = Entity.entity(content, MediaType.APPLICATION_JSON);

You can notice that the content is not necessarily to be a String, and the generic type of the entity is going to change accordingly.

Step 2. Configure an Invocation$Builder

To configure an Invocation$Builder, use ClientBuilder.newClient() to create a new Client object, configure the Client object, and build the Invocation$Builder:

  1. Create a new Client object;
  2. Configure the Client object;
  3. Build the Invocation$Builder.
// Create a new Client object
Client client = ClientBuilder.newClient();

// Set an authentication (if needed)
client = client.register(new BasicAuthentication(username, password));

// Set the url, which can be a String or wrapped object
WebTarget target =;

// Build a builder
Builder builder = target.request();

Step 3. Use Invocation$Builder to send a request attached with the Entity.

Depending on what method you want to invoke on the server side, you can do GET, POST, PUT, DELETE, which generally are mapped to CRUD operations on the server side:

// To attach anything with GET method, modify the url
Response response = builder.get();

Response response =;

Response response = builder.put(entity);

Response response = builder.delete(entity);

See another example of using the ClientBuilder in a demo project.

This blog may not be up to date. Please see the original post from Blogger
via IFTTT.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s