Passing headers with axios POST request

后端 未结 9 1903
青春惊慌失措
青春惊慌失措 2020-11-28 20:27

I have written an axios POST request as recommended from the npm package documentation like:

var data = {
    \'key1\': \'val1\',
    \'key2\': \'val2\'
}
ax         


        
9条回答
  •  被撕碎了的回忆
    2020-11-28 20:47

    When using axios, in order to pass custom headers, supply an object containing the headers as the last argument

    Modify your axios request like:

    const headers = {
      'Content-Type': 'application/json',
      'Authorization': 'JWT fefege...'
    }
    
    axios.post(Helper.getUserAPI(), data, {
        headers: headers
      })
      .then((response) => {
        dispatch({
          type: FOUND_USER,
          data: response.data[0]
        })
      })
      .catch((error) => {
        dispatch({
          type: ERROR_FINDING_USER
        })
      })
    

提交回复
热议问题