简体   繁体   中英

TypeError: Network request failed for 204 (No Content) and 304 (Not Modified) call over HTTP2 (ERR_HTTP2_PROTOCOL_ERROR in Chrome)

We have switched from HTTP1.1 to HTTP2 protocol and have noticed that some requests fail. When a server responds with 204 or 304 we get the following error:

TypeError: Network request failed

Chrome additionally shows it with the following error on network tab: ERR_HTTP2_PROTOCOL_ERROR

After some research we have found what the issue was. HTTP2 protocol is stricter in it's syntax that HTTP1.1 is. Eg it does not allow body for 204 or 304 responses. It is logical that these responses should not contain body, but HTTP1.1 did not require to fail these "strange" responses while HTTP2 requires that a client does not accept such a response.

We have additionally found out that even though we did not have body or content-length header, we did have a content-type header which the browser does not accept either while the logic for HTTP1.1 was just ignoring this header.

I did not find much information about this on the Internet at all. The closest I got was the HTTP2 RFC , and even it was a bit vague on details. It does not state directly that a 304 response that has a content-type header should be rejected, but I assume that's how browsers have implemented it in reality.

TLDR:

204 and 304 responses cannot contain body, content-length or content-type header. Such responses will be treated as a network error by the browser (and probably other clients as well).

PS It is likely that there are similar restrictions for other response codes

PPS Kudos to Morten Cools who has done original research on the topic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM