415 Unsupported Media Type는 요청 본문의 형식(Content-Type)을 서버가 지원하지 않아 거부했다는 뜻입니다. 예를 들어 JSON만 받는 엔드포인트에 XML이나 text/plain을 보내면 발생합니다.
406 Not Acceptable이 '응답(서버가 만드는 것)' 협상 실패라면, 415는 '요청 본문(클라이언트가 보내는 것)'의 형식이 맞지 않는 경우라는 점이 다릅니다.
POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: text/xml
<user><name>Kim</name></user>HTTP/1.1 415 Unsupported Media Type
Accept-Post: application/json
Content-Type: application/json
{"error":"unsupported_media_type","expected":"application/json"}app.post('/api/users', (req, res) => {
if (!req.is('application/json')) {
return res.status(415)
.set('Accept-Post', 'application/json')
.json({ error: 'unsupported_media_type' });
}
// ...
});curl -X POST https://api.example.com/api/users \
-H 'Content-Type: application/json' \
-d '{"name":"Kim"}'