Golo Roden
Golo Roden

Reputation: 150972

Request's content-type header contains more than the MIME type, why?

I noticed that sometimes the content-type header of a request (e.g., made by Firefox) does not only contain information on the MIME type, but also on the encoding.

E.g., when sending JSON using AJAX instead of

application/json

(what I expected) Firefox sent:

application/json; charset=UTF-8

I have a number of questions on this behavior:

  1. What other "parameters" (or "options", or whatever you call the charset value) could happen?
  2. Will application/json always come first, or may the values appear in random order (i.e., could it also be charset=UTF-8; application/json)?
  3. Is the separator always a ;?
  4. What is the canonical way of testing for a specific MIME type? Just comparing the header doesn't work, obviously. Any hints? Is there something better than contentType === 'application/json || contentType.startsWith('application/json;')?

PS: For question #4, I created a separate question. See Get an entire string or a substring, depending on a specific character

Upvotes: 0

Views: 233

Answers (1)

Alex K.
Alex K.

Reputation: 176006

See the HTTP 1.1 RFC

3.7 Media Types

HTTP uses Internet Media Types [17] in the Content-Type (section 14.17) and Accept (section 14.1) header fields in order to provide open and extensible data typing and type negotiation.

   media-type     = type "/" subtype *( ";" parameter )
   type           = token
   subtype        = token Parameters MAY follow the type/subtype in the form of attribute/value pairs (as defined in section 3.6).

So

  1. There is no fixed list of possible values
  2. Yes, type should come first
  3. The delimiter is ;

Upvotes: 1

Related Questions