Restassured Selenium
Restassured Selenium
GET
request in our earlier tutorials. We have also discussed validations like
validating headers and the status of the response obtained from the server.
In this article, we'll discuss the next method in REST API i.e. the POST request
using Rest Assured library. We'll cover the following topics in this article.
We use the verb "Post" everywhere when we are dealing with the web. For
example, when we are submitting any registration form on a particular
webpage like Gmail. We provide the required data and click submit. So
through this action of submitting data, we are actually POSTING or sending
the data to the server. The verb "POST" in HTTP, also called as POST request
method in HTTP sends data to the server. In our example, we are sending our
names, email and decided password as data.
Have a look at the "curl" command that has a POST request with all the data.
The operation is successful and hence we get the response code as 201.
As far as real-world applications are concerned, the POST request can have a
very big size and also a complex body structure. Since it provides an extra
layer of security, we often use it for passing sensitive business-related data. It
is also noteworthy here that the POST request does not always require all the
data to be filled by the user. It completely depends on the server
implementation and therefore you should always go through the
documentation before.
In Rest Assured, we make use of the post() method to make an HTTP POST
request.
For making an HTTP Post request using Rest Assured, let's add a simple JSON
library in our classpath so that we can create JSON objects in the code. We
can use the following URL to download simple JSON from the
Maven: https://mvnrepository.com/artifact/com.googlecode.json-simple/
json-simple. Once the jar is downloaded we can add it to the classpath.
Following are the steps we'll follow to make a POST Request using Rest
Assured.
RestAssured.baseURI = "https://demoqa.com/BookStore/v1/Books";
RequestSpecification request = RestAssured.given();
In the above code, we initialize a base URI with a link to the bookstore and
the 'createUser' API. Next, we create a 'request' using RequestSpecification.
Next, we will create a JSON request object that will contain the data we need
to create a new user. Given below is the code for the same:
Now that we have created the JSON string with the required data, the next
step will be to add this JSON to the request body and send or post the
request. Look at the following code:
// Add a header stating the Request body is a JSON
request.header("Content-Type", "application/json"); // Add the Json to the
body of the request
request.body(requestParams.toJSONString()); // Post the request and check
the response
So in this step, we simply add the JSON String to the body of the HTTP
Request and set the Content-Type header field value to application/JSON.
Next, we use the method RequestSpecification.body(JsonString) to put the
JSON body into the request. Using this method we can update the content of
the HTTP Request Body.
Next using the post () method on the request object we send this data to the
server using the 'BookStoreV1BooksPost' API.
After posting the request we have to validate the response we received from
the server as a result of a POST request. Given below is the code for
validating the response:
}
When we execute the above code, we get the following response.
We can clearly see the output says the incorrect usage of the HTTP Request
Method. Similarly, we have other negative scenarios listed below which we will
leave to users to try themselves.
You can try the above scenarios on the same URL used above to demonstrate
the POST request.
Note: Corresponding Postman tutorial for Post request can be found at Response
in Postman.