Why does fetch().json() fail on HTTP 204?
Because 204 responses carry zero bytes in the body. Check `if (res.status === 204) return null;` before calling `res.json()`.
HTTP 204 No Content signals success with an intentionally empty response body. It is the gold standard for CORS OPTIONS preflight responses, DELETE operations, and beacon endpoints.
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin "https://app.example.com" always;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
add_header Access-Control-Allow-Headers "Authorization, Content-Type" always;
add_header Access-Control-Max-Age 86400 always;
return 204;
}
curl -i -X OPTIONS -H 'Origin: https://app.example.com' -H 'Access-Control-Request-Method: POST' https://api.example.com/v1/items
Because 204 responses carry zero bytes in the body. Check `if (res.status === 204) return null;` before calling `res.json()`.
204 No Content is preferred by the Fetch specification because it avoids unnecessary payload parsing; legacy IE11 clients sometimes used 200.
RFC 9110 forbids sending a Content-Length or Transfer-Encoding header with a non-zero body on 204 responses.