Frontend/React

[React/Axios] axios delete 요청 시 body에 data 넣는 방법

_성호_ 2022. 10. 20. 12:41
728x90
반응형

🚨 오류 발생

AxiosError message: 'Request failed with status code 400'
axios.delete(`${url}/unlike`, {
   projectId: id,
 })

axios의 delete를 사용하여 서버에 요청을 할 때, 다음과 같이 data를 전달하면 오류가 발생한다.

 

💡 해결 방법

axios.delete는 request body를 지원하며 다음과 같이 두개의 매개변수를 허용한다.

axios.delete(url[, config])

 

👍 config.data는 request body를 설정하는데 사용할 수 있다.

axios.delete(`${url}/unlike`, {
  data: {
    projectId: id,
    },
 })

2번째 인자에서 data를 data: {} 형태로 한 번 더 감싸주면서 오류를 해결❗

 

💻 참고한 사이트

https://github.com/axios/axios/issues/897

 

Support delete body · Issue #897 · axios/axios

This is probably a design choice by the axios team and for good reason but I write all my POST/GET/PUTs like this: const res = await axios.post(url, { data: { ... } }) And my server sometimes expec...

github.com