본문 바로가기
개발/에러해결

req.body 가 비었을 때(empty)

by amkorousagi 2023. 5. 26.

req.body empty 에러 내용


node fetch로 post 메서드로 request를 보냈는데 express에서 까보니 req.body가 비어있다.

req.body empty 에러 원인


Content-Type 헤더를 안 적어서 req.body가 json인 줄 모른다.

 

req.body empty 해결 방법


다음과 같이 Content-Type header도 적어준다.

 

javascript
fetch(URL, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(body), }) .then((res) => { return res.json(); })

 

댓글