23API TestingAPI TestingHTTP
Explain HTTP Methods (GET, POST, PUT, DELETE, PATCH)
1. GET:
- Purpose: Retrieve data from server
- Idempotent: Yes (same result on multiple calls)
- Safe: Yes (doesn’t modify data)
- Cacheable: Yes
- Body: No request body
- Example: GET /api/users/123
2. POST:
- Purpose: Create new resource
- Idempotent: No (creates new resource each time)
- Safe: No (modifies data)
- Cacheable: Rarely
- Body: Contains data to create
- Example: POST /api/users (with user data in body)
3. PUT:
- Purpose: Update/replace entire resource
- Idempotent: Yes
- Safe: No
- Cacheable: No
- Body: Contains complete updated data
- Example: PUT /api/users/123 (replaces entire user)
4. PATCH:
- Purpose: Partial update of resource
- Idempotent: Can be (depends on implementation)
- Safe: No
- Cacheable: No
- Body: Contains only fields to update
- Example: PATCH /api/users/123 (updates specific fields)
5. DELETE:
- Purpose: Remove resource
- Idempotent: Yes
- Safe: No
- Cacheable: No
- Body: Usually no body
- Example: DELETE /api/users/123
PUT vs PATCH Example
PUT Request (replaces entire resource):
PUT /api/users/123
{
"name": "John Smith",
"email": "john.smith@example.com",
"age": 31
}
PATCH Request (updates only specified fields):
PATCH /api/users/123
{
"name": "John Smith"
}