Activity - Use methods with curl
This is an activity from the Documenting APIs online course. For this activity, we will be using curl and the Swagger Petstore.
Create a new pet
We will create a new pet by passing a JSON file with a curl command.
- Insert the following JSON code into a text file:
{
"id":555,
"category":{
"id":0,
"name":"string"
},
"name":"doctordog",
"photoUrls":[
"string"
],
"tags":[
{
"id":0,
"name":"string"
}
],
"status":"available"
}
-d
(send data) parameter of the curl request.
- Change the first
id
value to another integer and change thename
value to another name. - Save the the file as
mypet.json
in a local directory. - Open a command prompt from the directory.
- After your terminal or command prompt is in the same directory as your JSON file, create the new pet with the following curl request:
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d @mypet.json "https://petstore.swagger.io/v2/pet"
X POST
: create (POST
) a new resource.--header
: include extra or custom headers.Content-Type
: the type of content to submit with the request.Accept
: the type of content we will accept in the response.-d @mypet.json
: send data/file.
The response should look like this and contain the name of the newly created pet:
{"id":555,"category":{"id":0,"name":"string"},"name":"doctordog","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}
Update the name of your pet
We will change our pet's name with the update pet method.
- Open the
mypet.json
file and change thename
value. - Use the
PUT
(update) method instead of thePOST
(create) method. - Update the name of the pet with the following curl request:
curl -X PUT --header "Content-Type: application/json" --header "Accept: application/json" -d @mypet.json "https://petstore.swagger.io/v2/pet"
The response should look like this and contain the updated pet name:
{"id":555,"category":{"id":0,"name":"string"},"name":"misterdog","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}
Get your pet's name by ID
We will search for our pet by passing the id
into the /pet/{petID}
endpoint:
- Copy the first
id
value from themypet.json
file. - Use this curl command to get information about the pet:
curl -X GET --header "Accept: application/json" "https://petstore.swagger.io/v2/pet/555"
The response should look like this and contain the name of the pet:
{"id":555,"category":{"id":0,"name":"string"},"name":"misterdog","photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"}
Delete your pet
We will delete our pet from the registry.
- Delete the pet from the registry with following curl request:
curl -X DELETE --header "Accept: application/json" "https://petstore.swagger.io/v2/pet/555"
- The response should look like this:
{"code":200,"type":"unknown","message":"555"}
- Confirm the pet has been deleted from the registry with this curl command:
curl -X GET --header "Accept: application/json" "https://petstore.swagger.io/v2/pet/555"
The response should look like this:
{"code":1,"type":"error","message":"Pet not found"}
This activity demonstrates how to perform CRUD (create, read, update, delete) operations with curl requests.
How import a curl request into Postman
We will confirm our pet has been deleted from the petstore registry by importing our previous curl request into Postman.
- Open Postman.
- Click Import.
- Click Raw text.
-
Copy and paste the following curl request from the previous activity (Delete your pet).
curl -X GET --header "Accept: application/json" "https://petstore.swagger.io/v2/pet/555"
-
Click Continue.
- The Import Entities tab will open.
- Click Import.
-
The curl request has been converted to the following
GET
request in Postman:https://petstore.swagger.io/v2/pet/555
-
Click Send to complete the process.
You should see the same message from Step 4 of the previous activity (Delete your pet).