Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
REST Assured
3.
Features of PATCH Request
4.
Different Response Codes
5.
PATCH request in REST assured
6.
Frequently Asked Questions
6.1.
What is RequestSpecBuilder?
6.2.
What is REST API testing?
6.3.
Why do we use REST assured?
6.4.
What makes a RESTful service?
6.5.
Why is REST API called REST?
7.
Conclusion 
Last Updated: Mar 27, 2024

Let’s Write First PATCH Request in REST Assured

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.

Let’s Write First 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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- 

installing the JSON server

Step. 2

After installing the JSON server, you need to create a db.json file using the following command-

create a db.json file

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-

default data

Step. 3

Now that the default file has been created, you can modify this file accordingly.  

modified file

Now on hitting the URL http://localhost:3000 we can see the modified data.

modified  data in url

Step. 4

Now, we will require eclipse to create a PATCH request in REST Assured. For that, we will first set up the project following the given steps.

  1. Click on this link and install eclipse. Select Eclipse IDE for Java Developers.  
installing eclipse
  1. Select the installation folder. Once it is installed, select your work directory.
selecting folder
  1. Click on this link and generate a project like this-
generate a project
  1. Now extract the project file and import it into eclipse. For that-

Go to import->Maven->Existing Maven Project.

extract the project file
  1. Now, a window will pop up where you have to give the root directory and click on finish. Hurray! The project is created, it will look like this-
project directory

Step. 5

To use REST assured in your project, you must include the dependencies listed below. Now go to the pom.xml file, edit as shown below, and save it.

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
           <dependency>
                 <groupId>io.rest-assured</groupId>
                 <artifactId>rest-assured</artifactId>
                 <version>4.1.2</version>
            </dependency>
    <dependency>
     <groupId>javax.xml.bind</groupId>
     <artifactId>jaxb-api</artifactId>
     <version>2.3.0</version>
  </dependency>
 </dependencies>

Alright! We are done with setting up the project. Now let us write the first PATCH Request in Rest Assured.

Step. 6

The next step is to create a class PatchMethod.java by right clicking on the package name com.codingNinjas.AnuradhaDixit.Student->New->Class.

 creating a class

Step. 7

Now that we have created the class, we need to import certain libraries to write the first PATCH request in REST assured. These libraries are-

import static io.restassured.RestAssured.*;
import io.restassured.http.ContentType;

Step. 8

Now we can proceed with the code implementation as shown in the given screenshot-

code implementation

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-

output

Step. 9

Now you can patch the data using the given code-

Patched data

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-

Modified code

The same modifications will be reflected in the browser after refreshing the URL.

modified code in 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:

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Keep Coding!

Previous article
Let Us Write First PUT Request in REST Assured
Next article
Let’s Write First DELETE Request in REST Assured
Live masterclass