http://www.technicalpage.net/search/label/SQL

RestAssuredPost

 Rest Assured Post - TestNG

package restAssuredTest;

 

import static io.restassured.RestAssured.given;

import static org.hamcrest.Matchers.equalTo;

import static org.hamcrest.Matchers.hasItems;

 

import java.util.HashMap;

import java.util.Map;

 

import org.json.simple.JSONObject;

import org.testng.annotations.Test;

 

public class PostRequest { //in post request you have to pass the body

      

       @Test

       void postTest() {

//           Map<String, Object> map = new HashMap<String, Object>();

//           map.put("name", "John");//input parameter for post method

//           map.put("job", "helper");//input parameter for post method

//           System.out.println("The request is: "+map);

//           //we need json library to show the result in json.

//           //we can use gson dependency or jackson dependency or json dependency or json simple library for json

//           //we are using json simple library

//          

//           JSONObject jsonRequest = new JSONObject(map);

//           System.out.println("The json request is: "+jsonRequest);

//           System.out.println("The json jsonRequest.toJSONString is: "+jsonRequest.toJSONString());//this shows same output like above line

//          

             //Above code can be written like below with out map

             //---------------------------

             JSONObject jsonRequest = new JSONObject();

             jsonRequest.put("name", "John");//input parameter for post method

             jsonRequest.put("job", "helper");//input parameter for post method

            

             System.out.println("The json request is: "+jsonRequest);

             System.out.println("The json jsonRequest.toJSONString is: "+jsonRequest.toJSONString());//this shows same output like above line

             //------------------------------

             given()

             //.header("Key","Value");//if you need to give header, give here like this

             //such as .header("Content-Type","application/json")

             //.contentType(ContentType.JSON)

             //.accept(ContentType.JSON)

             .body(jsonRequest.toJSONString())

             .when()

                    .post("https://reqres.in/api/users")

             .then()

             .statusCode(201);

       } 

}

 

When you test the URL https://reqres.in/api/users  with below inputs, you get below data:


 

 

RestAssuredGET

Rest Get API (TestNG)

package restAssuredTest;

 

import org.testng.Assert;

import org.testng.annotations.Test;

//import io.restassured.RestAssured; //for first test case

import static io.restassured.RestAssured.*;//for second test case

import io.restassured.response.Response;

import java.net.*;

 

//Below imports are for test case 2

import static io.restassured.matcher.RestAssuredMatchers.* ;

import static org.hamcrest.Matchers.* ;

 

public class GetRequest {

      

       @Test

       public void getTest() {

 

             //Response response =  RestAssured.get("https://reqres.in/api/users?page=2");

             Response response =  get("https://reqres.in/api/users?page=2"); //we can directly use above line like this after importing static io.restassured.RestAssured.*;

             System.out.println("---------------------------response.asString()--------------");

             System.out.println(response.asString());

             System.out.println("---------------------------response.getBody().asString()--------------");

             System.out.println(response.getBody().asString());

             System.out.println("---------------------------response.getStatusCode()--------------");

             System.out.println(response.getStatusCode());

             System.out.println("---------------------------response.getStatusLine()--------------");

             System.out.println(response.getStatusLine());

             System.out.println("---------------------------response.getHeader(\"content-type\")--------------");

             System.out.println(response.getHeader("content-type"));

             System.out.println("-----------------------response.getTime()------------------");

             System.out.println(response.getTime());

            

             int statusCode = response.getStatusCode();

             Assert.assertEquals(statusCode, 200, "This is not right status code");

            

                          

       }

      

       @Test

       public void getTestAnotherWay() {

             given()

             //.header("Key","Value")//if you need to give header, give here like this

             //such as .header("Content-Type","application/json")

             //.param("Key","Value")//if you need to give parameter, give here like this

             .get("https://reqres.in/api/users?page=2")

             .then()

                    .statusCode(200)

                    .body("data.id[0]",equalTo(7))//[0] is the index

                    .body("data.id[4]",equalTo(11))

                    .log().all()//this will show all the response on the console log

                    .body("data.first_name", hasItems("Lindsay","Byron")); //check in the collection [Michael, Lindsay, Tobias, Byron, George, Rachel]

            

 

       }

}

 

When you test the URL https://reqres.in/api/users?page=2  , you get below data: