close

[Solved] How to set header and options in axios?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How to set header and options in axios? in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How to set header and options in axios Error Occurs?

Today I get the following error How to set header and options in axios? in javascript.

How to Solve set header and options in axios Error ?

  1. How to Solve set header and options in axios Error ?

    to Solve set header and options in axios Error axios({
    method: 'post',
    url: '….',
    params: {'HTTP_CONTENT_LANGUAGE': self.language},
    headers: {'header1': value}
    })

  2. How to set header and options in axios?

    to Solve set header and options in axios Error axios({
    method: 'post',
    url: '….',
    params: {'HTTP_CONTENT_LANGUAGE': self.language},
    headers: {'header1': value}
    })

Solution 1

You can pass a config object to axios like:

axios({
  method: 'post',
  url: '....',
  params: {'HTTP_CONTENT_LANGUAGE': self.language},
  headers: {'header1': value}
})

Solution 2

You can initialize a default header axios.defaults.headers

 axios.defaults.headers = {
        'Content-Type': 'application/json',
        Authorization: 'myspecialpassword'
    }

   axios.post('https://myapi.com', { data: "hello world" })
        .then(response => {
            console.log('Response', response.data)
        })
        .catch(e => {
            console.log('Error: ', e.response.data)
        })

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment