2023-07-05 15:11:16 +02:00
|
|
|
---
|
|
|
|
title: Invalid API Route Status/Body Response
|
|
|
|
---
|
2021-08-25 17:52:43 +02:00
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Why This Error Occurred
|
2021-08-25 17:52:43 +02:00
|
|
|
|
|
|
|
In one of your API routes a 204 or 304 status code was used as well as sending a response body.
|
|
|
|
|
|
|
|
This is invalid as a 204 or 304 status code dictates no response body should be present.
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Possible Ways to Fix It
|
2021-08-25 17:52:43 +02:00
|
|
|
|
|
|
|
Send an empty body when using a 204 or 304 status code or use a different status code while sending a response body.
|
|
|
|
|
|
|
|
Before
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
```js filename="pages/api/example.js"
|
2021-08-25 17:52:43 +02:00
|
|
|
export default function handler(req, res) {
|
|
|
|
res.status(204).send('invalid body')
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
After
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
```js filename="pages/api/example.js"
|
2021-08-25 17:52:43 +02:00
|
|
|
export default function handler(req, res) {
|
2022-02-03 03:41:28 +01:00
|
|
|
res.status(204).end()
|
2021-08-25 17:52:43 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Useful Links
|
2021-08-25 17:52:43 +02:00
|
|
|
|
2023-09-13 18:06:29 +02:00
|
|
|
- [204 status code documentation](https://developer.mozilla.org/docs/Web/HTTP/Status/204)
|
|
|
|
- [304 status code documentation](https://developer.mozilla.org/docs/Web/HTTP/Status/304)
|