Setting Request Headers With Axios - Mastering JS

Mastering JS Logo Mastering JS Tutorials Newsletter eBooks Jobs ☰ Tutorials Newsletter eBooks Jobs Tutorials / Axios / Setting Request Headers with Axios Apr 27, 2019

To set HTTP request headers with an axios GET request, you should pass an object with a headers property as the 2nd argument.

const axios = require('axios'); // httpbin.org gives you the headers in the response // body `res.data`. // See: https://httpbin.org/#/HTTP_Methods/get_get const res = await axios.get('https://httpbin.org/get', { headers: { 'Test-Header': 'test-value' } }); res.data.headers['Test-Header']; // "test-value"

With PUT and POST requests, the 2nd argument is the request body, so you should pass an object with a headers property as the 3rd argument.

const res = await axios.post('https://httpbin.org/post', { hello: 'world' }, { headers: { 'Test-Header': 'test-value' } }); res.data.headers['Test-Header']; // "test-value" Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Axios Tutorials

  • Configuring maxBodyLength in Axios
  • How to Send Headers With an Axios POST Request
  • HTTP DELETE Requests with Body in Axios
  • How to Use JSON with Axios
  • How to Use the User-Agent Header in Axios
  • Axios Multipart Form Data
  • How to use Axios' create() Method with POST Requests

Mastering Axios

  • GET Requests
  • Get the HTTP Response Body
  • POST Requests
  • PUT Requests
  • DELETE Requests
  • The then() Function
  • Error Handling using catch()
  • Calling Axios as a Function

Framework Features

  • The create() Function
  • Axios Interceptors

Integrations

  • Basic Auth

Tag » Add Custom Headers In Axios