Rest Assured: Working with REST API made easy!


Modern web applications commonly utilize web services which provide a web-based interface to database server. Representational State Transfer (REST) is an architecture for well-behaved web services that can function at an Internet-scale.
Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain. It supports POST, GET, PUT, DELETE, OPTIONS, PATCH, and HEAD requests and can be used to validate and verify the response of these requests.
To get a basic understanding of Rest Assured, do read this blog here. And to look over various REST Calls continue reading this blog, where I will be covering POST, PUT, GET & DELETE Request.

1) POST Request

HTTP POST Verb often called POST Method is used to send data to the Server. The data that is sent to the server in a POST request is sent in the body of the HTTP request. The type of body, XML, JSON or some other format is defined by the Content-Type header. If a POST request contains JSON data then the Content-Type header will have a value of application/JSON. Similarly, for a POST request containing XML, the Content-Type header value will be application/XML.

Steps for POST Request:

Step 1: Set BaseURI & BasePath (change accordingly)
baseURI = localhost
basePath = /api/upload
Step 2: Create Body
{
  “FirstName” : “value”
  “LastName” : “value”
}
Step3: Now make a Request as:
 Response postRequest = given().headers(
                 "Content-Type",
                 ContentType.JSON,
                 "Accept",
                 ContentType.JSON)
            .when().body().post().then().using().extract().response();
Step 4: Check the status:
 int status = postRequest.getStatusCode();

2) PUT Request

The HTTP PUT request method creates a new resource or substitutes a representation of the target resource with the request payload.
According to the official HTTP RFC specifies PUT to be:
  • A PUT method puts a file or resource at a specific URI, and precisely at that URI.
  • If a file or a resource already exists at that URI, PUT replaces that file or resource.
  • In addition to the above, Responses to this method are not cacheable.
Below example will show how to put a Binary File on specific URI using PUT

Steps for PUT Request:

Step 1: Set BaseURI & BasePath (change accordingly)
baseURI = localhost
basePath = /api/put
Step 2: Create Body:
In this case, the body takes the file as input which is to be uploaded:
File file = new File("abc.txt")
Step 3: Now make a PUT Request
If uploading on S3 bucket, add this configuration while making a PUT Request:
.config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
Response uploadFileUrlResponse = given().contentType("text/CSV") 
 .config(RestAssured.config()
.encoderConfig(EncoderConfig.encoderConfig()
.appendDefaultContentCharsetToContentTypeIfUndefined(false))) 
                 .body(File)
                 .when()
                 .put()
                 .then().extract().response();
Step 4: Check the status:
 int status = uploadFileUrlResponse.getStatusCode();

3) GET Request

Steps for GET Request:

Step 1: Set BaseURI & BasePath (change accordingly)
baseURI = localhost
basePath = /api/get/{jobId} //pathParam
Let’s say you need to create one of your endpoints to be data-driven over the REST Call, so that can be done by passing as pathParam as shown below:
Step2: Make a GET Request
Response jobStatusRequest = given().pathParam("jobId", jobId).when().get().then().using().extract().response();
Step 3: Check the status:
 int status = jobStatusRequest.getStatusCode();

4) DELETE Request

The DELETE method requests the origin server to delete the resource identified by the Request-URI.
The server implementation determines if the actual content will be deleted or not.
DELETE Request can be done as follows:
Step 1: Set BaseURI & BasePath (change accordingly)
baseURI = localhost
basePath = /api/delete
Step2: Make a GET Request
Response deleteRequest = given().headers(
                 "Content-Type",
                 ContentType.JSON,
                 "Accept",
                 ContentType.JSON)
                 .when().delete().then().using().extract().response();
Step 3: Check the status:
 int status = deleteRequest.getStatusCode();
So, this is how all such API calls can be made using REST Assured.
If you like this blog, please do show your appreciation by hitting like button and sharing this blog. Also, drop any comments about the post & improvements if needed. Till then HAPPY LEARNING.

Comments