Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Rest Api

Uploaded by

ashiqmohamed1907
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Rest Api

Uploaded by

ashiqmohamed1907
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Can a GET request be used to create a resource instead of PUT?

Use the GET option because it has view-only rights. The POST or PUT methods should never be used
to create a resource.

Why do we use static import in Rest


Assured?
Ans:
Static import is a Java programming language feature that
allows members (fields and methods) that have been scoped
as public static within their container class to be used in Java
code without mentioning the class in which the field
has been defined.

Program to test Rest API using Rest Assured?

public class EmployeesTest {

@Test
public void GetAllEmoloyees()
{
// base URL to call
RestAssured.baseURI =
"http://localhost:8080/employees/get";

//Provide HTTP method type - GET, and URL to get


all employees
//This will give respose
Response employeesResponse =
RestAssured.given().request(Method.GET, "/all");

// Print the response in string format


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

}
}

Google's Gson library


Gson gson = new Gson();
List<Employee> returnedEmployees = gson.fromJson(jsonStr, new
TypeToken<List<Employee>>(){}.getType());

How can we get size of JSON array in


Rest assured?
Ans:
JSON Response:
------------------
{
"Status": 200,
"ORG": {
"EMPLOYEES": [
{
"id": 1,
"name": XYZ,
"role": "ADMIN"
},
{
"id": 2,
"name": ABC,
"role": "USER"
},
{
"id": 3,
"name": AAA,
"role": "USER"
}
]
}
}
// base URL to call
RestAssured.baseURI =
"http://localhost:8080/employees/get";

//Provide HTTP method type - GET, and URL to get


all employees
//This will give respose
Response employeesResponse =
RestAssured.given().request(Method.GET, "/all");

//use JsonPath from Rest-Assured to get list of


employee id
List<String> employees =
employeesResponse.jsonPath().getList("ORG.EMPLOYEES
.id");
System.out.println(employees.size());
Most commonly used status codes are:

 200 - success/OK
 201 - CREATED - used in POST or PUT methods.
 304 - NOT MODIFIED - used in conditional GET requests to reduce the
bandwidth use of the network. Here, the body of the response sent should
be empty.
 400 - BAD REQUEST - This can be due to validation errors or missing input
data.
 401- UNAUTHORIZED - This is returned when there is no valid
authentication credentials sent along with the request.
 403 - FORBIDDEN - sent when the user does not have access (or is
forbidden) to the resource.
 404 - NOT FOUND - Resource method is not available.
 500 - INTERNAL SERVER ERROR - server threw some exceptions while
running the method.
 502 - BAD GATEWAY - Server was not able to get the response from
another upstream server.

What are the HTTP Methods?

HTTP Methods are also known as HTTP Verbs. They form a major portion
of uniform interface restriction followed by the REST that specifies what
action has to be followed to get the requested resource. Below are some
examples of HTTP Methods:

 GET: This is used for fetching details from the server and is basically a
read-only operation.
 POST: This method is used for the creation of new resources on the server.
 PUT: This method is used to update the old/existing resource on the server
or to replace the resource.
 DELETE: This method is used to delete the resource on the server.
 PATCH: This is used for modifying the resource on the server.
 OPTIONS: This fetches the list of supported options of resources present on
the server.

The POST, GET, PUT, DELETE corresponds to the create, read, update,
delete operations which are most commonly called CRUD Operations.

Can you tell what constitutes the core components of


HTTP Request?

In REST, any HTTP Request has 5 main components, they are:

 Method/Verb − This part tells what methods the request operation


represents. Methods like GET, PUT, POST, DELETE, etc are some examples.
 URI − This part is used for uniquely identifying the resources on the server.
 HTTP Version − This part indicates what version of HTTP protocol you are
using. An example can be HTTP v1.1.
 Request Header − This part has the details of the request metadata such
as client type, the content format supported, message format, cache
settings, etc.
 Request Body − This part represents the actual message content to be
sent to the server.

What constitutes the core components of HTTP Response?

HTTP Response has 4 components:

 Response Status Code − This represents the server


response status code for the requested resource. Example-
400 represents a client-side error, 200 represents a
successful response.
 HTTP Version − Indicates the HTTP protocol version.
 Response Header − This part has the metadata of the
response message. Data can describe what is the content
length, content type, response date, what is server type,
etc.
 Response Body − This part contains what is the actual
resource/message returned from the server.

You might also like