Test API with Postman
So now that we have the CRUD API calls setup in the api side. How do we know it works? From here, we can either believe in our godly programming skills and start building the front-end, or we can use a rest client to test the calls. In this tutorial, we will be using Postman, although we welcome any other options as well.
Preemptive
Before we start, lets setup the headers so it'll not bother us for the most part:This will tell our HTTP request to notify the server we want JSON as the response format, and our locale is in english (en)
Create
Method: POST
URL: {baseUrl}/api/{controllerName}
Add this in the body tab right below the URL bar:
{
"name": "Chicken Fried Rice",
"ingredients": [
{
"name": "Chicken",
"amount": 2,
"unit": "lb"
},
{
"name": "Rice",
"amount": 1.5,
"unit": "cups"
},
{
"name": "Oil",
"amount": 2,
"unit": "tsp"
}
]
}
Result:
{
"__v": 0,
"name": "Chicken Fried Rice",
"_id": "592e7c142305f84010f60780",
"ingredients": [
{
"name": "Chicken",
"unit": "lb",
"_id": "592e7c142305f84010f60783",
"amount": 2
},
{
"name": "Rice",
"unit": "cups",
"_id": "592e7c142305f84010f60782",
"amount": 1.5
},
{
"name": "Oil",
"unit": "tsp",
"_id": "592e7c142305f84010f60781",
"amount": 2
}
]
}
Get All
Method: GET
URL: {baseUrl}/api/{controllerName}
Result:
[
{
"_id": "592e7c142305f84010f60780",
"name": "Chicken Fried Rice",
"ingredients": [
{
"name": "Chicken",
"unit": "lb",
"_id": "592e7c142305f84010f60783",
"amount": 2
},
{
"name": "Rice",
"unit": "cups",
"_id": "592e7c142305f84010f60782",
"amount": 1.5
},
{
"name": "Oil",
"unit": "tsp",
"_id": "592e7c142305f84010f60781",
"amount": 2
}
],
"__v": 0
}
]
Get By Id
Method: GET
URL: {baseUrl}/api/{controllerName}/{_id}
Result:
{
"_id": "592e7c142305f84010f60780",
"name": "Chicken Fried Rice",
"ingredients": [
{
"name": "Chicken",
"unit": "lb",
"_id": "592e7c142305f84010f60783",
"amount": 1.5
},
{
"name": "Rice",
"unit": "cups",
"_id": "592e7c142305f84010f60782",
"amount": 1.5
},
{
"name": "Oil",
"unit": "tsp",
"_id": "592e7c142305f84010f60781",
"amount": 2
}
],
"__v": 0
}
Update
Method: PUT
URL: {baseUrl}/api/{controllerName}/{_id}
Body:
{
"_id": "592e7c142305f84010f60780",
"name": "Chicken Fried Rice 2.0",
"ingredients": [
{
"name": "Chicken",
"amount": 2,
"unit": "lb"
},
{
"name": "Rice",
"amount": 1.5,
"unit": "cups"
},
{
"name": "Oil",
"amount": 2,
"unit": "tsp"
}
]
}
Result:
{
"_id": "592e7c142305f84010f60780",
"name": "Chicken Fried Rice 2.0",
"ingredients": [
{
"name": "Chicken",
"amount": 2,
"unit": "lb"
},
{
"name": "Rice",
"amount": 1.5,
"unit": "cups"
},
{
"name": "Oil",
"amount": 2,
"unit": "tsp"
}
]
}
Delete
Method: DELETE
URL: {baseUrl}/api/{controllerName}/{_id}
Result:
{
"_id": "592e7c142305f84010f60780",
"name": "Chicken Fried Rice 2.0",
"__v": 0,
"ingredients": [
{
"_id": "592e7e392305f84010f60786",
"unit": "lb",
"name": "Chicken",
"amount": 2
},
{
"_id": "592e7e392305f84010f60785",
"unit": "cups",
"name": "Rice",
"amount": 1.5
},
{
"_id": "592e7e392305f84010f60784",
"unit": "tsp",
"name": "Oil",
"amount": 2
}
]
}