Axios Delete request with body and headers?

后端 未结 11 2086
梦谈多话
梦谈多话 2020-11-28 08:16

I\'m using Axios while programing in ReactJS and I pretend to send a DELETE request to my server.

To do so I need the headers:

headers: {
  \'Authori         


        
相关标签:
11条回答
  • 2020-11-28 08:45

    Here is a brief summary of the formats required to send various http verbs with axios:

    • GET: Two ways

      • First method

        axios.get('/user?ID=12345')
          .then(function (response) {
            // Do something
          })
        
      • Second method

        axios.get('/user', {
            params: {
              ID: 12345
            }
          })
          .then(function (response) {
            // Do something
          })
        

      The two above are equivalent. Observe the params keyword in the second method.

    • POST and PATCH

      axios.post('any-url', payload).then(
        // payload is the body of the request
        // Do something
      )
      
      axios.patch('any-url', payload).then(
        // payload is the body of the request
        // Do something
      )
      
    • DELETE

      axios.delete('url', { data: payload }).then(
        // Observe the data keyword this time. Very important
        // payload is the request body
        // Do something
      )
      

    Key take aways

    • get requests optionally need a params key to properly set query parameters
    • delete requests with a body need it to be set under a data key
    0 讨论(0)
  • 2020-11-28 08:49

    I had the same issue I solved it like that:

    axios.delete(url, {data:{username:"user", password:"pass"}, headers:{Authorization: "token"}})
    
    0 讨论(0)
  • 2020-11-28 08:49

    i found a way that's works:

    axios
          .delete(URL, {
            params: { id: 'IDDataBase'},
            headers: {
              token: 'TOKEN',
            },
          }) 
          .then(function (response) {
            
          })
          .catch(function (error) {
            console.log(error);
          });
    

    I hope this work for you too.

    0 讨论(0)
  • 2020-11-28 08:51

    So after a number of tries, I found it working.

    Please follow the order sequence it's very important else it won't work

    axios.delete(URL, {
      headers: {
        Authorization: authorizationToken
      },
      data: {
        source: source
      }
    });
    
    0 讨论(0)
  • 2020-11-28 08:55

    Actually, axios.delete supports a request body.
    It accepts two parameters: a URL and an optional config. That is...

    axios.delete(url: string, config?: AxiosRequestConfig | undefined)
    

    You can do the following to set the response body for the delete request:

    let config = { 
        headers: {
            Authorization: authToken
        },
        data: { //! Take note of the `data` keyword. This is the request body.
            key: value,
            ... //! more `key: value` pairs as desired.
        } 
    }
    
    axios.delete(url, config)
    

    I hope this helps someone!

    0 讨论(0)
  • 2020-11-28 09:03

    For Delete, you will need to do as per the following

    axios.delete("/<your endpoint>", { data:<"payload object">})
    

    It worked for me.

    0 讨论(0)
提交回复
热议问题