Axios
Call a REST API with the Axios library, whose get, post, put and delete functions set the method, the JSON header and the error check for you.
Apprentice · JavaScript · REST API · Updated
The Axios library simplifies making API calls because it wraps common configurations for each CRUD operation into its own function.
Add Axios to your page with this script tag, which loads it from a CDN:
<script src="https://cdn.jsdelivr.net/npm/axios@1.7.7/dist/axios.min.js"></script>
Below are examples for the various REST CRUD operations using the Axios library.
GET
Note that we do not have to check for the OK (200) status, Axios handles that check for us.
const baseUrl = 'http://localhost:8080/api/categories';
function getAllCategories()
{
return axios.get(baseUrl)
.then(response => response.data);
}
POST
Note that Axios handles changing the method to post, updates the header to specify JSON as the content type, and stringifies the body.
const baseUrl = 'http://localhost:8080/api/categories';
function addNewCategory(category)
{
return axios.post(baseUrl, category)
.then(response => response.data);
}
PUT
Note that like in the POST method, axios handles all of the configuration, we just need to change the URL to include the ID.
const baseUrl = 'http://localhost:8080/api/categories';
function updateCategory(id, category)
{
const url = `${baseUrl}/${id}`;
return axios.put(url, category);
}
DELETE
Note that the DELETE method does not have a body.
const baseUrl = 'http://localhost:8080/api/categories';
function deleteCategory(id)
{
const url = `${baseUrl}/${id}`;
return axios.delete(url);
}
Do it: add a product and delete one with Axios
The products page needs two calls to the products API: one to add a new product and one to delete a product. With Axios, each is one line of work.
The practice box can't load Axios from a CDN, so its page holds a small stand-in that works like Axios's get, post, put and delete (you can read it in the page's HTML). It has no real API either, so it answers the requests itself, the way your products API would:
| The request | The API answers |
|---|---|
POST http://localhost:8080/api/products |
201, with the saved product, its new id included: { "productId": 78, "productName": "Trail Mix", "unitPrice": 4.5 } |
DELETE http://localhost:8080/api/products/78 |
204: deleted, with no body |
DELETE http://localhost:8080/api/products/999 |
404: there's no such product |
The two functions get these values:
product: the new product, an object withproductNameandunitPrice, such as{ productName: 'Trail Mix', unitPrice: 4.5 }.id: the id of the product to delete, a number, such as78.
For example, addProduct({ productName: 'Trail Mix', unitPrice: 4.5 }) sends a POST to http://localhost:8080/api/products with the product as its JSON body. The promise it returns gives back the saved product:
{ productId: 78, productName: 'Trail Mix', unitPrice: 4.5 }
Your job: write two functions with Axios, each returning its promise.
addProduct(product)posts the product tobaseUrl, and its promise gives back the saved product (the response's data).deleteProduct(id)sends aDELETEtobaseUrl, a slash, then the id:http://localhost:8080/api/products/78.- When the API answers
404,deleteProduct's promise fails (Axios does that for you; there's nothing to check).
Practiced live on the lesson page: write the JavaScript right there and check it.