Get method in Rest Assured

  • GET method: from rest
  • get() method: from rest assured

The GET method is used to extract information from the given server using a given URI. While using GET request, it should only extract data and should have no other effect on the data.

get() of Rest assured to access the get method of rest. So using get() method from the rest assured, we can call the GET method and we can fetch the data from the server or from the database.

import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
public class FirstTest {
	@Test
	void test() {		
		RestAssured.given()
		.when()
		.get("https://chercher.tech/sample/api/product/read")
		.prettyPrint();
	}
}

output-get-method-rest-assured

Post Method in RestAssured

POST method in rest API is used to update an already existing resource. We can use the post method present in a Rest assured class to connect with the post() method and update the existing resource in the database/server.

To update a resource we might need to know the content type it is accepting; for example, few resources will accept JSON format and a few others might accept different formats.

So based on your resources you have to define the content type. Once you have the content type then you have to form the body to send. This body is nothing but the fields that you want to update and along with the unique identifier.

Based on this unique identifier only we are going to update the resource, In my case ID is the unique identifier.

API end point : https://chercher.tech/sample/api/product/update

import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class FirstPostTest extends Base{
	@Test
	void test() {
		RestAssured.given()
		.when()
		.contentType(ContentType.JSON)
		.body("{
" + 
				"	"id":"2405",
" + 
				"	"name": "Latest gadg1233et",
" + 
				"	"description": "New version is released",
" + 
				"	"price": "10"
" + 
				"}")
		
		.post("https://chercher.tech/sample/api/product/update")
		.prettyPrint();
	}
}

rest-assured-post-method

You can see the updated details on this web page: https://chercher.tech/sample/api-ui
updated-post-rest-assured

Video

Put Method in RestAssured

The put() method in the rest assured will create a new resource in the database/server. Unlike the post() method we do not need any unique identifier while sending the body.

import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class FirstPostTest extends Base{
	@Test
	void test() {
		RestAssured.given()
		.when()
		.contentType(ContentType.JSON)
		.body("{
" + 
				"	"name": "put method",
" + 
				"	"description": "example for put method",
" + 
				"	"price": "10"
" + 
				"}")
		
		.put("https://chercher.tech/sample/api/product/create")
		.prettyPrint();
	}
}

put-method-rest-assured
pur-method-rest-assured

Delete method in Rest Assured

The delete() method will remove a resource from the database/server. To delete a server we need to pass a unique identifier to identify the resource.

End point : https://chercher.tech/sample/api/product/delete

public class FirstPostTest extends Base{
	@Test
	void test() {
		RestAssured.given()
		.when()
		.contentType(ContentType.JSON)
		.body("{
" + 
				"	"id": "2407"
" + 
				"}")
		
		.delete("https://chercher.tech/sample/api/product/delete")
		.prettyPrint();
	}
}

delete-method-rest-assured

Patch Method in Rest Assured

patch() method will update a specific field in a resource; incase of post method it will update all the fields of a resource.

As of the moment, our API does not support the Patch method.
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions