Axios Options

Sep 9, 2020

The 2nd parameter to axios.get() and 3rd parameter to axios.post() and axios.put() is an options object, also known as the Axios request config. You can find a complete list of options on the Axios docs.

Below is a list of the most important options. Click on an option to read more about it.

url

If you use a helper function like axios.get() or axios.post(), Axios automatically sets this option for you. But you can also set the url by using the axios() function, which takes the request config as its first parameter.

const axios = require('axios');

// Equivalent to `axios.get('https://httpbin.org/get')`
const res = await axios({
  url: 'https://httpbin.org/get',
  method: 'get'
});

method

Helper functions like axios.get() and axios.post() automatically set the method for you, but you can also set it in your request config:

const axios = require('axios');

// Equivalent to `axios.post('https://httpbin.org/post')`
const res = await axios({
  url: 'https://httpbin.org/post',
  method: 'post'
});

data

Axios serializes the data option into the HTTP request body. This option only works with POST, PUT, DELETE, and PATCH requests. Setting data is a no-op for GET requests.

const axios = require('axios');

// Equivalent to `axios.post('https://httpbin.org/post', { answer: 42 })`
const res = await axios({
  url: 'https://httpbin.org/post',
  method: 'post',
  data: { answer: 42 }
});

res.data.json; // { answer: 42 }

params

Axios serializes the params option into the request's query string.

const axios = require('axios');

// Equivalent to `axios.get('https://httpbin.org/get?answer=42')`
const res = await axios.get('https://httpbin.org/get', { params: { answer: 42 } });

res.data.args; // { answer: 42 }

baseURL

This option is often used with axios.create() to ensure that the server URL you're sending requests to is defined in only one place, as opposed to having to copy/paste https://api.myservice.com repeatedly. For example:

const axios = require('axios').create({
  baseURL: 'https://httpbin.org'
});

// Sends request to 'https://httpbin.org/get' 
const res = await axios.get('/get?hello=world');

More Axios Tutorials