HTTP Protocol Content Negotiation

To Kin Hang
1 min readNov 24, 2020

--

Just found there is a mechanism in HTTP protocol (Content Negotiation, RFC7231) to let user agents specify which types of the return they are preferred. #old_news_is_so_excited

By specifying preferred MIME types in an HTTP Accept header, servers may return difference formats back to clients. For example, an HTML file will be returned to browsers (note 1) or a JSON to Postman/curl.

Note 1: Technically speaking, I mean agents which accept: text/html. Since modern browsers insert default accept values, it comes for free. ‘q’ stands for quality values, a decimal number between 0.000 (not acceptable) to 1.000 (most preferred, default value).

Source code: https://www.youtube.com/watch?v=LEdEOlLlMfM
An Example of supplying HTML and JSON options for user agents in Haskell Yesod https://www.youtube.com/watch?v=LEdEOlLlMfM

Given the default accept values of media, we can handle the compatibility issues for WebP (and HEIV/AVIF in future) in the HTTP level on backend servers instead of modifying frontend <picture> stuff. (Yet, it is still a good option, see Google’s doc on WebP for more.)

Browsers’ default Accept values for image type

--

--

No responses yet