Axios instance
Dec 19, 2022
In the JavaScript library Axios, an instance is an object that represents a connection to a specific server or service. It can be configured with various settings, such as the base URL for requests, headers, and other options.
Here’s an example of creating an Axios instance:
const instance = axios.create({
baseURL: 'https://my-api.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
Once you have an Axios instance, you can use it to send HTTP requests. For example, to send a GET request to the /users
endpoint of the API, you can use the get
method:
instance.get('/users').then(response => {
// handle the response here
});
You can also use the other HTTP methods provided by Axios, such as post
, put
, and delete
, to send requests with different HTTP verbs.
instance.post('/users', { name: 'John Smith' }).then(response => {
// handle the response here
});