Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix handling for 204 status code with a body (#28479)
This ensures we handle 204 and 304 status codes correctly in API routes. ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md` Fixes: #28464
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Invalid API Route Status/Body Response | ||
|
||
#### Why This Error Occurred | ||
|
||
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. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Send an empty body when using a 204 or 304 status code or use a different status code while sending a response body. | ||
|
||
Before | ||
|
||
```js | ||
export default function handler(req, res) { | ||
res.status(204).send('invalid body') | ||
} | ||
``` | ||
|
||
After | ||
|
||
```js | ||
export default function handler(req, res) { | ||
res.status(204).send() | ||
} | ||
``` | ||
|
||
### Useful Links | ||
|
||
- [204 status code documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204) | ||
- [304 status code documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function handler(req, res) { | ||
if (req.query.invalid) { | ||
// test the warning when content is added for a 204 response | ||
return res.status(204).json({ hello: 'world' }) | ||
} | ||
return res.status(204).send() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters