Axios Delete request with body and headers?

后端 未结 11 2087
梦谈多话
梦谈多话 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 09:05

    I tried all of the above which did not work for me. I ended up just going with PUT (inspiration found here) and just changed my server side logic to perform a delete on this url call. (django rest framework function override).

    e.g.

    .put(`http://127.0.0.1:8006/api/updatetoken/20`, bayst)
          .then((response) => response.data)
          .catch((error) => { throw error.response.data; });
    
    0 讨论(0)
  • 2020-11-28 09:06

    axiox.delete does support a request body. It accepts two parameters: url and optional config. You can use config.data to set the request body and headers as follows:

    axios.delete(url, { data: { foo: "bar" }, headers: { "Authorization": "***" } });
    

    See Here - https://github.com/axios/axios/issues/897

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

    To send an HTTP DELETE with some headers via axios I've done this:

      const deleteUrl = "http//foo.bar.baz";
      const httpReqHeaders = {
        'Authorization': token,
        'Content-Type': 'application/json'
      };
      // check the structure here: https://github.com/axios/axios#request-config
      const axiosConfigObject = {headers: httpReqHeaders}; 
    
      axios.delete(deleteUrl, axiosConfigObject);
    

    The axios syntax for different HTTP verbs (GET, POST, PUT, DELETE) is tricky because sometimes the 2nd parameter is supposed to be the HTTP body, some other times (when it might not be needed) you just pass the headers as the 2nd parameter.

    However let's say you need to send an HTTP POST request without an HTTP body, then you need to pass undefined as the 2nd parameter.

    Bare in mind that according to the definition of the configuration object (https://github.com/axios/axios#request-config) you can still pass an HTTP body in the HTTP call via the data field when calling axios.delete, however for the HTTP DELETE verb it will be ignored.

    This confusion between the 2nd parameter being sometimes the HTTP body and some other time the whole config object for axios is due to how the HTTP rules have been implemented. Sometimes an HTTP body is not needed for an HTTP call to be considered valid.

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

    axios.delete is passed a url and an optional configuration.

    axios.delete(url[, config])

    The fields available to the configuration can include the headers.

    This makes it so that the API call can be written as:

    const headers = {
      'Authorization': 'Bearer paperboy'
    }
    const data = {
      foo: 'bar'
    }
    
    axios.delete('https://foo.svc/resource', {headers, data})
    
    0 讨论(0)
  • 2020-11-28 09:10

    I encountered the same problem... I solved it by creating a custom axios instance. and using that to make a authenticated delete request..

    const token = localStorage.getItem('token');
    const request = axios.create({
            headers: {
                Authorization: token
            }
        });
    
    await request.delete('<your route>, { data: { <your data> }});
    
    0 讨论(0)
提交回复
热议问题