SNoV
SNoV

Reputation: 97

Axios delete method with a payload

Im using axios on my react app to send a delete request, I need to send an id list as payload but it returns "415 Unsupported Media Type".

Here's my code:

const deviceData = ["31234"];

axios.delete(url, { data: deviceData }).then(res => {
  if (res.status === 200) {
    const pagination = { ...this.state.pagination };
    this.setState({
      loading: false,
      data: res.data.data.devices,
      pagination
    });
  }
});

Upvotes: 6

Views: 14481

Answers (1)

thortsx
thortsx

Reputation: 2280

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

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

See here to more information: https://github.com/axios/axios/issues/897#issuecomment-343715381

Or you can try to set header with: 'Content-Type': 'application/json; charset=utf-8'

const deviceData = ["31234"];

axios.delete(url,
  { headers:{'Content-Type': 'application/json; charset=utf-8'} },
  { data: { deviceData: deviceData } }).then(res => {
  if (res.status === 200) {
    const pagination = { ...this.state.pagination };
    this.setState({
      loading: false,
      data: res.data.data.devices,
      pagination
    });
  }
});

Upvotes: 17

Related Questions