Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have config parameter in axios post request with headers required

I was trying to send a post request using axios in my react code which needed both 'headers parameter' and 'config parameter' at the same time. I found out that there is two types for writing post requests in axios:

  1. axios.post(url, data, config)

  2. axios({ url :url, method: 'post', headers: headers, data: data })

In type 1 we can't send headers parameter and in type 2 we can't send config parameter.

So is there any way to solve this problem?

I solved it using xml httpRequest instead of axios, but I'm curious about the way we could solve it using axios.

like image 245
Parisa Payami Avatar asked Sep 11 '18 19:09

Parisa Payami


2 Answers

base on doc

you can set header in config !

axios.post(url, data, {headers : {'X-Requested-With': 'XMLHttpRequest'} })

or you can send all options as a object

axios.request ({
    url: '/user',
    method: 'post',
    data: {
        firstName: 'Fred'
    },
    headers: {'X-Requested-With': 'XMLHttpRequest'},

    // ... and other options 
})
like image 193
Neo Anderson Avatar answered Nov 06 '22 17:11

Neo Anderson


your assumption is wrong you can set the headers in the request config

https://github.com/axios/axios#request-config

{
   headers: {'X-Requested-With': 'XMLHttpRequest'},
   ...
}
like image 25
Olivier Boissé Avatar answered Nov 06 '22 18:11

Olivier Boissé