1. What are the different API methods you have tested?
Answer:
In API testing, I have tested several important HTTP methods that allow interaction with server resources. Each method serves a different purpose:
- GET:
- Retrieves data from the server without modifying it.
- It is a safe and idempotent method (repeating it gives the same result).
- Retrieves data from the server without modifying it.
Example:
java
given()
.when()
.get(“https://api.example.com/users”)
.then()
.statusCode(200);
- Use case: Fetching a list of users.
- POST:
- Sends data to the server to create a new resource.
- It is not idempotent (repeating may create multiple entries).
- Sends data to the server to create a new resource.
Example:
java
given()
.contentType(“application/json”)
.body(“{ \”name\”: \”John\”, \”age\”: 30 }”)
.when()
.post(“https://api.example.com/users”)
.then()
.statusCode(201);
- Use case: Creating a new user record.
- PUT:
- Updates an existing resource or creates it if it doesn’t exist.
- It is idempotent.
- Updates an existing resource or creates it if it doesn’t exist.
Example:
java
given()
.contentType(“application/json”)
.body(“{ \”name\”: \”John\”, \”age\”: 31 }”)
.when()
.put(“https://api.example.com/users/1”)
.then()
.statusCode(200);
- Use case: Updating a user’s profile.
- DELETE:
- Removes a resource from the server.
- Removes a resource from the server.
Example:
java
given()
.when()
.delete(“https://api.example.com/users/1”)
.then()
.statusCode(204);
- Use case: Deleting a user record.
- PATCH:
- Partially updates a resource.
- Only the specified fields are updated.
- Partially updates a resource.
Example:
java
given()
.contentType(“application/json”)
.body(“{ \”age\”: 32 }”)
.when()
.patch(“https://api.example.com/users/1”)
.then()
.statusCode(200);
- Use case: Updating only the age of a user.
2. What is the PUT method?
Answer:
The PUT method is mainly used to update an existing resource completely or create it if it doesn’t already exist.
- Idempotent Behavior:
- Making the same PUT request multiple times produces the same result (no duplicate records).
- Making the same PUT request multiple times produces the same result (no duplicate records).
- Common Use Case:
- Updating the details of a user, such as name or age.
- Updating the details of a user, such as name or age.
Example:
java
given()
.contentType(“application/json”)
.body(“{ \”name\”: \”John\”, \”age\”: 30 }”)
.when()
.put(“https://api.example.com/users/1”)
.then()
.statusCode(200);
-
- In this example, the user with ID 1 is updated with the provided details.
- In this example, the user with ID 1 is updated with the provided details.
- Important Points:
- If the resource does not exist, the server may create it (depending on API design).
- PUT usually requires sending the full updated entity in the request body.
- If the resource does not exist, the server may create it (depending on API design).
3. What type of payload does POST support?
Answer:
The POST method supports sending various types of payloads to the server. Common formats include:
- JSON (JavaScript Object Notation):
- Most widely used format for APIs.
- Lightweight and easy to read.
- Most widely used format for APIs.
Example:
java
given()
.contentType(“application/json”)
.body(“{ \”name\”: \”John\”, \”age\”: 30 }”)
.when()
.post(“https://api.example.com/users”)
.then()
.statusCode(201);
- XML (Extensible Markup Language):
- Used in older systems.
- More verbose compared to JSON.
- Used in older systems.
Example:
java
given()
.contentType(“application/xml”)
.body(“<user><name>John</name><age>30</age></user>”)
.when()
.post(“https://api.example.com/users”)
.then()
.statusCode(201);
- Form Data (application/x-www-form-urlencoded):
- Used in web form submissions.
- Used in web form submissions.
Example:
java
given()
.contentType(“application/x-www-form-urlencoded”)
.formParam(“name”, “John”)
.formParam(“age”, “30”)
.when()
.post(“https://api.example.com/users”)
.then()
.statusCode(201);
- Multipart Data (multipart/form-data):
- Used for file uploads.
- Used for file uploads.
Example:
java
given()
.multiPart(new File(“/path/to/file”))
.when()
.post(“https://api.example.com/upload”)
.then()
.statusCode(201);
- Key Takeaway:
- The server must know what content type is being sent, so setting the correct Content-Type header is very important.
- The server must know what content type is being sent, so setting the correct Content-Type header is very important.
4. Key difference between PUT and POST
Answer:
Feature | PUT | POST |
Purpose | Update existing resource or create if missing | Create a new resource |
Idempotency | Idempotent (same result on multiple calls) | Not idempotent (creates new every time) |
Payload Requirement | Requires full updated data | Can submit partial or full data |
Example Usage | Update user profile | Register a new user |
PUT Example:
java
given()
.contentType(“application/json”)
.body(“{ \”name\”: \”John\”, \”age\”: 30 }”)
.when()
.put(“https://api.example.com/users/1”)
.then()
.statusCode(200);
POST Example:
java
given()
.contentType(“application/json”)
.body(“{ \”name\”: \”John\”, \”age\”: 30 }”)
.when()
.post(“https://api.example.com/users”)
.then()
.statusCode(201);
- Summary:
- Use POST when creating a brand new resource.
- Use PUT when updating an existing one or creating it at a specific URL if it doesn’t exist.
- Use POST when creating a brand new resource.
5. Explain different data types in payload for API request
Answer:
APIs work with various types of data inside a payload (usually JSON or XML). These are the primary data types:
- String:
- Represents a sequence of characters (text).
- Represents a sequence of characters (text).
Example:
json
{ “name”: “John” }
- Number:
- Can be integer or floating point.
- Can be integer or floating point.
Example:
json
{ “age”: 30 }
- Boolean:
- Represents true or false values.
- Represents true or false values.
Example:
json
{ “isActive”: true }
- Array:
- A list of values enclosed in square brackets.
- A list of values enclosed in square brackets.
Example:
json
{ “tags”: [“developer”, “blogger”] }
- Object:
- A collection of key-value pairs (nested structures).
- A collection of key-value pairs (nested structures).
Example:
json
{
“address”: {
“street”: “123 Main St”,
“city”: “Anytown”
}
}
- Important Note:
- Complex APIs often involve sending a mixture of these types inside the request payload.
- Complex APIs often involve sending a mixture of these types inside the request payload.
👉The Next 5 Questions-2: API Method