01/

Representation 

Representation -> How resources are manipulated REST API uses JSON/XML for data. -> Payload We create request in the form of JSON/XML and we also get response from the RESTAPI in the form of JSON/XML only It is stateless -> Client data is not stored on the server, session data is also stored on the client It uses http/https protocol and it makes it language independent RESTAPI can be written in any language and developers in any language can consume it

02/

Endpoints

Endpoints contain: 1. URIs -> This is the essentially the endpoint (Address of the resource where to contact) Unique Resource Identifier 2. Resources -> Which specific thing we want - navigate to a page and then show 3. Parameters - Used to filter the resources. When we want to get specific data from the resource

03/

CRED 

Get -> Read -> Reading the information
Post -> Create -> New Information
Put -> Update -> Modifying exiting information
Delete -> Delete -> Delete information from the application

04/

Headers

Contains the information about request and response


What to verify?
1. Status Code
2. Response Message/Error Message
3. Few other response details

 

05/

RestAssured

given() -> Initialization / Auth
Login to application
Request header
Parameters, cookies
Body

When() -> Resources
GET(resource path)
POST(resource path)
PUT(resource path)
DELTE(resource path)

Then() -> Validation
validate status code
assert
Extract the response, header

Given I have this information
When I perform this action
Then this should be the output

06/

Query Parameters vs Path Parameters

• Query parameters: These are appended at the end of the RESTApi Endpoint. They can be identified by the use of ?
Example:
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=YOUR_API_KEY

• Path parameters: These are part of the RESTApi endpoint. 
Example:
https://maps.googleapis.com/maps/api/{json}?queryparameters
https://baseURI/{year}/sports

Form Parameters:
All the data from UI/Rest can go using two ways:
1. PayLoad -> Google Places
2. Form Parameters

https://stackoverflow.com/questions/11552248/when-to-use-queryparam-vs-pathparam

07/

JSON

Dictionary:
Key: Value

{ -> Starting Point
} -> Ending Point

[ -> Starting List
] -> Ending List
It means, that value is a list of items
Index -> Starting from 0 index

 

08/

String

Double quotes has a special meaning
1st double quotes represents start of string
2nd double quotes represent end of string

“ in between string ”

\ -> Escape Character, \” -> Escape special meaning of double quotes

API Demo

public class GetRequestDemo {

 

/*** * Given I have this information

When I perform this action

Then this should be the output */

 

@BeforeClass public void setup() {

RestAssured.baseURI = "https://maps.googleapis.com";

RestAssured.basePath = "/maps/api";

}

 

@Test public void statusCodeVerification() {

given()

.param("units", "imperial")

.param("origins", "Washington,DC")

.param("destinations", "New+York+City,NY")

.param("key", "AIzaSyAFNxOzcDNEZ9coJzPc_9N-CA8Euun2fDA")

 

.when() .get("/distancematrix/json")

.then()

.statusCode(200);

}

}

Get Response

public class TwitterExtractResponse {

 

String consumerKey = "ubhe5nnmztzlJMzdLihtKEgsT";

String consumerSecret = "KjDJDSJw2o882K71cK33PqUdRbIlp5DNxCxYknDrSvQwNsb2S1";

String accessToken = "886092793365409792-mBtgcXzcn9IOC1WmWp5c1fXMn3HDZHr";

String accessSecret = "jUIEOAnEiZLlICLBlpR6UhVKZ98CMpQ7banbfcP5gW3eS";

 

@BeforeClass public void setup() {

RestAssured.baseURI = "https://api.twitter.com"; RestAssured.basePath = "/1.1/statuses";

}

@Test public void extractResponse() {

Response response = given()

.auth()

.oauth(consumerKey, consumerSecret, accessToken, accessSecret) .queryParam("status", "My First Tweet")

.when()

.post("/update.json")

 

.then() .statusCode(200) .extract().response();

String id = response.path("id_str"); System.out.println(id);

}

}