Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Have you ever tested APIs? If yes, then you must be familiar with the API testing tools. Today we are going to discuss one such tool, which is REST Assured. Further we will also explore about PATCH request in REST assured.
REST (Representational State Transfer) is an architectural paradigm for establishing web services that define requirements. REST API is a straightforward and flexible approach to accessing online services without going through any processing.
Let's now move towards knowing more about REST assured.
REST Assured
Rest assured is a Java library that integrates well with Maven and enables us to test RESTful APIs. It is generally famous for testing JSON and XML-based web applications. Nearly every section of the request and response can be retrieved data using Rest Assured's techniques. REST Assured supports all REST methods, such as GET, PUT, POST, PATCH, and DELETE.
Let's now understand the process of writing the first PATCH request in REST assured.
Features of PATCH Request
An HTTP patch request is carried out using the method of HTTP patch, which allows the data update of particular fields of an entity.
A PATCH request is a set of instructions for modifying a resource.
PATCH is not cacheable.
We use the PUT request for complete updates and the PATCH request for partial updates.
It remains in the browser history and can also be bookmarked.
A server can declare that it supports PATCH by including it in the list in the response headers for Allow or Access-Control-Allow-Methods (for CORS). The PATCH request can execute one or multiple operations as a part of the JSON array.
Different Response Codes
A status code is a server's response to a browser. The browser generally sends a request to the server of the specified website when you enter a web address into the address bar. The server then returns the three-digit HTTP status code to the browser.
200
SUCCESS/OK
The response message indicates that the task was successful.
202
Accepted
The server has accepted the request, but it is still in processing.
301
Permanent redirect
The 301 status code indicates that the web page has been permanently replaced with some different resource.
302
Temporary redirect
This code generally indicates that the request sent by the browser for a web page was found, but the URL or content has been moved temporarily.
404
Not Found
When the server cannot find the particular resource. The reason could either not exist or have previously been deleted.
These were some of the most common HTTP errors. The proper knowledge of the status codes will help troubleshoot issues with your web servers or applications.
PATCH request in REST assured
Rest Assured uses the patch() method to send an HTTP PATCH request.
Before implementing the patch() method, we have to create a fake restful API using a JSON server. To install a JSON server, you need to follow the given steps.
Step. 1
The first step is installing the JSON server using the npm command-
Step. 2
After installing the JSON server, you need to create a db.json file using the following command-
In the above snapshot, we can see that the db.json file is created with some default data. On hitting the URL http://localhost:3000 you can view the default data in the file, which may look like this-
Step. 3
Now that the default file has been created, you can modify this file accordingly.
Now we can proceed with the code implementation as shown in the given screenshot-
After writing this code right click on PatchMethod.java class -> Run as->Java Application.
Now you can view the output in console as shown below-
Step. 9
Now you can patch the data using the given code-
Here as you can see, we have changed the firstName. In a similar way, you can modify the data according to your requirement.
Step. 10
After running the modified code, we can see the output in the console-
The same modifications will be reflected in the browser after refreshing the URL.
Frequently Asked Questions
What is RequestSpecBuilder?
RequestSpecBuilder is a class in Rest Assured. It contains methods that help in setting cookies, multipart details, header, body, authentication, query parameters, form parameters, path parameters, base URI, base path, proxy, etc.
What is REST API testing?
REST API Testing is an open-source technique for testing RESTful APIs in web applications. It is commonly used to test XML and JSON-based web applications. It works with all methods, including PUT, POST, PATCH, GET, and DELETE. REST API is a Java library.
Why do we use REST assured?
It eliminates the need to write a large amount of source code to establish an HTTP connection, send a request, and receive and parse a response.
What makes a RESTful service?
RESTful web services support total client-server separation. They simplify and decouple various server components so that each part can evolve independently. Platform or technology changes at the server application do not affect the client application.
Why is REST API called REST?
A REST API also referred to as a RESTful API, is a representational state transfer API that complies with the constraints of the REST architectural style and enables communication with RESTful web services.
Conclusion
In this article, we have extensively discussed what REST assured is, different types of requests, and how to write a PATCH request in Rest assured. To learn more about Rest API, check the link below: