R- Postman api
R- Postman api
HTTP method-:
1. POST -: It is use to create a resource.
2. GET -: It is use to read or retrieve the resource.
3. PUT -: It is use for complete updating of the resource.
4. PATCH -: It is use for partial updating of the resource.
5. DELETE-: It is use to delete the resource.
Payload -: Pass the input details to API/body
Status code-:
100-: Continue
200-: Success
201-: Successfully created
204 -: Successfully deleted.
300-: Redirected
422 -: Cannot create repository with the same name.
429-: Too Many Requests
500-: Server-side error
Client-side errors occur when the client sends a request that the server cannot process due to
issues originating from the client.
These errors indicate something is wrong with the request, not the server.
Examples:
Examples of Clients
1. Postman: Used to manually send API requests and analyze responses during testing.
2. Web Browser: Sends requests for web pages and retrieves responses.
3. Mobile Apps: For example, a weather app requesting data from a weather API.
4. Backend Systems: A payment gateway interacting with a bank’s API.
1. Who is Responsible?
The client is responsible for fixing the issue by correcting the request.
2. When Does It Happen?
It happens when the client:
o Sends incorrect or incomplete data.
o Uses invalid endpoints or HTTP methods.
o Wrong authentication.
Server-side errors occur when the server fails to process a valid request due to an issue on
the server side. These errors indicate problems with the server or the API logic.
Examples:
1. Who is Responsible?
The server owner or developer is responsible for fixing the issue by addressing server-
side bugs.
2. When Does It Happen?
It happens due to:
o Server misconfiguration.
o Unhandled exceptions in the application logic.
o Network or database failures.
Comparison:
Aspect Client-Side Errors (400) Server-Side Errors (500)
Problem in the request sent by Problem in the server's ability to process the
Origin of Issue
the client. request.
Missing parameters,
Examples Database failure, application crashes.
unauthorized access.
Common Status
400, 401, 403, 404, 422 500, 502, 503, 504
Codes
First, I make sure I have the Bearer Token, either from an API login or
an authentication endpoint. [ Query parameter and path parameter ]
In Postman, I create a new request, set the request method (like GET
or POST), and enter the API URL.
I then go to the Authorization tab, select Bearer Token from
the dropdown, and paste the token in the field provided.
After that, when I hit Send, Postman automatically adds the token in
the Authorization header as Bearer <token>.
API Key: Add API keys directly to the request as a query parameter or header.
Q. What are Postman Collections and Monitors, and how do they work
together?
Collections are groups of API requests that can be run together or
individually.
Monitors are used to schedule automated runs of these collections at
specific intervals (e.g., hourly, daily). They allow you to:
Continuously check the health of your APIs.
Receive alerts when a request fails.
Monitor can be set up from the Postman collection page with integrated
test scripts for automated validation.
Q. Imagine you have an API that returns a list of users. How would you
validate that the response contains the expected data?
Q. Describe how you would debug an API request that is returning an error.
Check the status code: Ensure the correct endpoint and HTTP method
are used.
Inspect headers: Look at Authorization, Content-Type, and other
required headers.
Examine the request payload: Ensure correct JSON or form data is sent.
Use Postman Console: View the request logs, including headers and full
payload.
Run in Collection Runner: Repeat the request and verify logs/output.
/*
pm.test("Content-Type header is application/json", ()=>{
pm.expect(pm.response.headers.get('Content-Type')).to.eql
('application/json; charset=utf-8');
});*/
Testing the Cookies response-: We can verify them by their value and
presence of coolies.
jsonData.forEach((item) => {
pm.expect(item).to.be.an("object"); // Each item
should be an object
pm.expect(item.id).to.be.a("string");
pm.expect(item.name).to.be.a("string");
pm.expect(item.location).to.be.a("string");
pm.expect(item.mobile).to.be.a("string");
pm.expect(item.designation).to.be.an("array"); //
Check if designation is an array
});
});
Assertion array properties -:
Check if an array is empty and if it contains particular items
pm.test("Test array properties", ()=>{
pm.expect(jsonData).to.be.an("array");
jsonData.forEach((item) => {
pm.expect(item.designation).to.have.members(["QA","SDET"])
;
});
});
Validate JSON field in response
{
"id": "02c4",
"name": "Rajat Kumar",
"location": "India",
"mobile": "34876598",
"designation": [
"SDET",
"Automation"
]
}
Validate JSON Schema
We need to genearate schema file, then need to save in a variable
like var or const. Then validate using tv4
tv4 – tini validator version number 4 -: (postman library )
Generate JSON Schema -: https://jsonschema.net/
var Schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"type": "object",
"required": ["id", "name", "location", "mobile",
"designation"],
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"location": {
"type": "string"
},
"mobile": {
"type": "string"
},
"designation": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
Scripts-:
Pre-request scripts
Tests
Pre-Request script Request Response Tests
Collection
Folder
Request
Variable -:
Global -: Accessible in workspace
Collection-: Accessible within collection
Environment-: Accessible in all collections, but we need to
switch to particular environment.
Local variable -: Accessible only within request (specific to
request)
Data variable -: External files csv/text
Referring variable using -: {{variable_name}}
Chaining of API’s-: Response from one API and request for another
API
GoRest API-:
URL-: https://gorest.co.in/
Token -: 612fb2ef78fcac307cd3a3bff004d80a511f1cfe79cda744f65c1a585eed2761
Request Body-:
{
"id": 7537304,
"name": "Datta Chaturvedi DC",
"email": "[email protected]",
"gender": "female",
"status": "active"
}
POST Create user
Pre-request script
var random = Math.random().toString(36).substring(2);
var userEmail = "jhon"+random+"@gmail.com";
var userName = "Jhon"+random;
pm.environment.set("email_env", userEmail);
pm.environment.set("name_env", userName);
Post-request script
var jsonData = pm.response.json();
pm.environment.set("userid_env", jsonData.id);
})
pm.environment.set("email_env", userEmail);
pm.environment.set("name_env", userName);
Post request
var jsonData=pm.response.json();
pm.environment.unset("email_env");
pm.environment.unset("userEmail");
pm.environment.unset("userName");
BOOKs API-:
"accessToken":
"44abc663cbeb49112913d91abbadb2b7d39cd94510e798842c584a3c69809c61"