Usually prior to executing a REST method, you will need to set some special data to the request besides the URI.
Add headers to the request
// the type of content we are sending client.addRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ); // the type of content we will accept back client.addRequestHeader( "Accept", "application/json" );
Add query parameters to the request
client.addRequestParameter( "lang", "English" );
Encoding of the request parameters
It is recommended, that you always encode values before adding them as request parameters.
You may use this method java.net.URLEncoder.encode(paramStr, “UTF-8”).
This is required if the value of GET request parameter contains white-spaces or other non-alphabet/non-digit characters.
For example:
client.addRequestParameter( "lang", "English UK" );
must be encoded like this:
client.addRequestParameter( "lang", java.net.URLEncoder.encode("English UK", “UTF-8”) );
Specify request media type
// send as text/xml client.setRequestMediaType( RestMediaType.TEXT_XML ); // send as application/xml client.setRequestMediaType( RestMediaType.APPLICATION_XML ); // send as application/json client.setRequestMediaType( RestMediaType.APPLICATION_JSON );
Basic authorization
client.setBasicAuthorization( USER_NAME, USER_PASSWORD );
Navigate to internal resource
// make an instance of the REST client by specifying the target URI RestClient client = new RestClient( <target URI>/people ); // add one or more resource path tokens client.addResourcePath( "delete" ); client.addResourcePath( "123" ); // execute a REST method RestResponse response = client.delete();
This example is equivalent to:
// make an instance of the REST client by specifying the target URI RestClient client = new RestClient( <target URI>/people/delete/123 ); // execute a REST method RestResponse response = client.delete();
Back to parent page
Go to Table of Contents