API concepts.
API concepts.
API is a way of communication between two application where application may differ
in their platforms or in terms of technology.
* Types of API?
* API VS Webservice
--------------------
after the completion of development and testing process of APIs
when the API is moved to production environment (on internet for end user) then it
is as web service.(required internet).
All webservices are APIs but all APIs are not webservices.
* Sample API:
https://reqres.in/api/users?page=2 URI/URL
https:// protocol
reqres.in host or domain
/api/users? Page=2 endpoints
/api/user? path parameter
page=2 query parameter
page key
2 value
2. json-server
npm install -g json-server
3. create .json file & run this json file by giving command
C:\Users\shubh\Downloads>json-server Student.Json
--------------------------
now your json file is running & you can run the apis
json array [ , ]
e.g.
{
"name": "john"' -string
"last name": null -null
"age": 30, -int
"mob no's": [78548,7888458] -array
"status": true -Boolean
} --json object
* Student data
---------------
{
"studens":[
{
"S_id": 101,
"S_name": "shubham",
"grad":"A"
},
{
"S_id": 102,
"S_name": "kartik",
"grad":"B"
},
{
"S_id": 103,
"S_name": "keshav",
"grad":"C"
}
]
}
*json path
----------
{
"students":[
{
"S_id": 101, -->students[0].S_id
"S_name": "shubham", -->students[0].S_name
"grad":"A"
},
{
"S_id": 102, -->students[1].S_id
"S_name": "kartik", -->students[1].S_name
"grad":"B"
},
{
"S_id": 103, -->students[2].S_id
"S_name": "keshav", -->student[2].S_name
"grad":"C"
}
]
}
http://jsonpathfinder.com
http://jsonpath.com
*Responce validation
status code
headers
cookies
Response time
response body
normal function
------------------
pm.test("test name", function()
{
//assertion;
}
);
arrow function
----------------
pm.test("test name", () =>
{
//assertion;
}
);
# Pre-request scripts
collection level
folder level
request level
*variables
what ?
why?
where?
scop of variable
-----------------
workspace-->collection-->request level
{{ VARIABLE }}
#Chaining of APIs
-to capture the response body into variable we use the library
JSON.parse(responebody); & store it into any variable
var jsondata=JSON.parse(responeBody);
gorest.co.in
-------------
URL: https://gorest.co.in
endpoints:
58b5d1a7caba758f40dcc6e40a6bc616cab573a78cf15a9e615527d8c49fbca0
request body
{
"name": "Tenali Ramakrishna",
"gender": "male",
"email":"tenali.ramakrishna@15ce.com",
"status": "active"
}
> to automate the process of random name and email we have add pre-request script
math.random.tostring(36).substring(2);
---- ---------------- -----------
library method submethod
- this will create the the random string and we can store it in variable
var name=random;
var email=random+"@gmail.com";
- and then store into env var
- now we can use request body as follow, which will auto generate name & email
every time when we run the request.
request body
{
"name": "{{name_env}}",
"gender": "male",
"email":"{{email_env}}",
"status": "active"
}
> now after the post request sent, we have to capture the "id" generated.
- so first we capture the response data & store the response date into variable
JSON.pasre(responceBody);
var jsondata=JSON.pasre(responceBody); // we can write in single statement
pm.environment.set.("id_env",jsondata.id);
> post request is done. now for get request we can use the env var {{id_env}} in
url
- we have to validate this response to the the data we posted in the post request
}
);
> now in put request we have modify data every time we run the request, so we add
same pre-request to as added in post request.
pm.environment.set.("name_env", name);
pm.environment.set.("email_env", email);
request body
{
"name": "{{name_env}}",
"gender": "male",
"email":"{{email_env}}",
"status": "active"
}
> in last delete if we want to delete the env var we declare then write script to
unset the var.
pm.environmenr.unset("id_env");
pm.environmenr.unset("name_env");
pm.environmenr.unset("email_env");
> in the post request we can use variable {{ }} and the value of this variable is
taken from the external file like csv,.json, text..
> we can upload the file at the time of collection run and at that time no of
itration we have to select(no of records from file)
# file uploading
post- httos//localhost:8080/uploadsinglefile
post- httos//localhost:8080/uploadmultiplefile
# Authentication types:
// cURL- client url, which contain all the parameter with url like endpoints,
request body....