What is Axios HTTP Client?
How to install Axios?
npm install axios yarn add axios
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
POST Request in Axios Code Example
To perform a POST request you can make use of the "post" method available from the "axios" object. Do also note that Axios is a promise-based library so it supports async/await syntax.
<script> import axios from 'axios'; const response = await axios .post('https://jsonplaceholder.typicode.com/posts', { title: 'foo', body: 'bar', userId: 1, }, { headers: { 'Content-type': 'application/json; charset=UTF-8' } }); console.log(response); </script>
https://jsonplaceholder.typicode.com/posts
Axios POST Body Data
To specify the body data you can pass in an object of values as the second parameter.
const data = { title: 'foo', body: 'bar', userId: 1, } axios.post('your-url-here', data);
Axios POST Headers Configuration
axios.post('your-url-here', 'your-data-here', { headers: { 'Content-type': 'application/json; charset=UTF-8' } });
Axios POST with "then" method
To retrieve the response through the "then" keyword you can write your code like below. By making use of this keyword you won't have to use the "async" and "await" anymore.
axios .post('your-url', 'your-data', 'your-headers') .then((data) => console.log(data))
Leave a reply