Practice Exercise: API CRUD Operations using Postman and cURL
Objective
Perform CRUD operations on a JSONPlaceholder API.
Prerequisites
-
Access to Linux-based machine. Follow the steps in Lab Environment to access your machine.
-
Postman: Make sure you have Postman installed on your local 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
Observations:
If the request is successful, you should receive a response with a status code (e.g., 201 Created).
The response will often include the newly created resource's details, such as an ID or other attributes.
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 -X GET https://jsonplaceholder.typicode.com/users
Observation:
If successful, you will receive a response with a status code (e.g., 200 OK) and the requested data in the response body.
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
Observation:
If successful, you will receive a response with a status code (e.g., 200 OK) and the updated resource's details
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
Observation:
If successful, you will receive a response with a status code (e.g., 204 No Content) indicating that the resource has been deleted.
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.
These are basic examples of CRUD operations using Postman and cURL. Depending on the specific API and its endpoints, the results and response structures may vary, so always refer to the API's documentation for accurate details and error handling.