Practice Exercise 1: API CRUD Operations using Postman and cURL
Objective
Perform CRUD operations on a JSONPlaceholder API.
Prerequisites
- Postman: Make sure you have Postman installed on your computer. You can download it from https://www.postman.com/.
- cURL: cURL is a command-line tool for making HTTP requests. You can install it if it's not already on your system. You can find installation instructions at https://curl.se/download.html.
Exercises
In this exercise, you'll perform CRUD operations on the JSONPlaceholder API using both Postman and cURL. You'll interact with a set of fake data representing users.
Exercise 1: Create (POST)
Postman:
- Open Postman.
- Create a new request and set the request type to POST.
- Set the request URL to https://jsonplaceholder.typicode.com/users.
- In the request body, select "Raw" and choose "JSON (application/json)" as the format.
- Provide a JSON object representing a new user.
{
"name": "John Doe",
"username": "johndoe",
"email": "johndoe@example.com"
}
cURL: - Open your terminal. - Run the following cURL command to create a new user:
curl -X POST -H "Content-Type: application/json" -d '{
"name": "John Doe",
"username": "johndoe",
"email": "johndoe@example.com"
}' https://jsonplaceholder.typicode.com/users
Exercise 2: READ (GET)
Postman: - Create a new request in Postman and set the request type to GET. - Set the request URL to https://jsonplaceholder.typicode.com/users. - Click the "Send" button to retrieve a list of users.
cURL: - Run the following cURL command to retrieve a list of users:
curl https://jsonplaceholder.typicode.com/users
Exercise 3: Update (PUT)
Postman: - Create a new request in Postman and set the request type to PUT. - Set the request URL to https://jsonplaceholder.typicode.com/users/1 to update the user with ID 1. - In the request body, select "Raw" and choose "JSON (application/json)" as the format. - Provide the updated user data.
{
"name": "Updated John Doe",
"username": "updatedjohndoe",
"email": "updatedjohndoe@example.com"
}
cURL:
- Run the following cURL command to update the user with ID 1:
curl -X PUT -H "Content-Type: application/json" -d '{ "name": "Updated John Doe", "username": "updatedjohndoe", "email": "updatedjohndoe@example.com" }' https://jsonplaceholder.typicode.com/users/1
Exercise 4: Delete (DELETE)
Postman:
- Create a new request in Postman and set the request type to DELETE.
- Set the request URL to https://jsonplaceholder.typicode.com/users/1 to delete the user with ID 1.
- Click the "Send" button to delete the user.
cURL:
- Run the following cURL command to delete the user with ID 1:
curl -X DELETE https://jsonplaceholder.typicode.com/users/1
Conclusion
These exercises will help you practice basic CRUD operations using Postman and cURL with the JSONPlaceholder API. You can adapt them to other APIs as well.