The Browser Extensions Standard (which is based on the Chrome Extension API) includes a webRequest API with methods onBeforeSendHeaders and onHeadersReceived, allowing a browser extension to see and modify HTTP headers in requests and responses.

The HTTP headers are provided as an array of objects. Each object has a name (e.g. 'content-type') and then it has either a value (e.g. 'text/html') or a binaryValue (represented as an array of integers). According to MDN you get a binaryValue only if the value "cannot be represented by UTF-8". But how can that actually happen? Under what circumstances can there exist an HTTP header whose value is not representable as a UTF-8 string?

I can't find a single example in the wild, or even any mention online, of binary HTTP header values. For example, in Node's built-in http2 module, the response.setHeader(name, value) method only accepts a string (or an array of strings) for the value.


For example it can be text in an encoding different from UTF-8. Since UTF-8 is aimed to be the only allowed encoding in language implemetations, texts in different encodings can be handled only as binary data.

Nodejs's library does not allow setting it probably because it was considered not practically interesting. But a standard implementation on the reading side should support all possible data.

| improve this answer | |
  • Interesting... Do you know if the HTTP spec takes any position on whether header values 'should' always be UTF-8 strings? I'm trying to understand if there are any valid use cases for binary header values that my extension should support (it's an extension that shows headers in a GUI). – callum Feb 21 '19 at 14:28
  • rfc2616 does not seem to say it (see definitions of header-content, then TEXT), but there may be some later corrections – max630 Feb 21 '19 at 14:42
  • You could show binary data as escaped octals or something like that. You probably do not plan to write them back, so there is no need for reverse transformation – max630 Feb 21 '19 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.