I have to validate the Content-Type header value before passing it to HTTP request.

Is there a specific list for all the possible values of Content-Type?

Otherwise, is there a way to validate the content type before using it in HTTP request?

AwesomeAwesome

4 Answers

You can find every content type here:http://www.iana.org/assignments/media-types/media-types.xhtml

The most common type are:

  1. Type application

  2. Type audio

  3. Type image

  4. Type multipart

  5. Type text

  6. Type video

  7. Type vnd :

lebarillierlebarillier

As is defined in RFC 1341:

In the Extended BNF notation of RFC 822, a Content-Type header field value is defined as follows:

Content-Type := type '/' subtype *[';' parameter]

type := 'application' / 'audio' / 'image' / 'message' / 'multipart' / 'text' / 'video' / x-token

x-token := < The two characters 'X-' followed, with no intervening white space, by any token >

subtype := token

parameter := attribute '=' value

attribute := token

value := token / quoted-string

token := 1*

tspecials := '(' / ')' / '<' / '>' / '@' ; Must be in / ',' / ';' / ':' / ' / <'> ; quoted-string, / '/' / '[' / ']' / '?' / '.' ; to use within / '=' ; parameter values

And a list of known MIME types that can follow it (or, as Joe remarks, the IANA source).

As you can see the list is way too big for you to validate against all of them. What you can do is validate against the general format and the type attribute to make sure that is correct (the set of options is small) and just assume that what follows it is correct (and of course catch any exceptions you might encounter when you put it to actual use).

Also note the comment above:

If another primary type is to be used for any reason, it must be given a name starting with 'X-' to indicate its non-standard status and to avoid any potential conflict with a future official name.

You'll notice that a lot of HTTP requests/responses include an X- header of some sort which are self defined, keep this in mind when validating the types.

Jeroen VannevelJeroen Vannevel

I would aim at covering a subset of possible 'Content-type' values, you question seems to focus on identifying known content types.

@Jeroen RFC 1341 reference is great, but for an fairly exhaustive list IANA keepsa web page of officially registered media types here.

Pete HornsbyPete Hornsby

If you are using jaxrs or any other, then there will be a class called mediatype.User interceptor before sending the request and compare it against this.

geddamsatishgeddamsatish

Not the answer you're looking for? Browse other questions tagged httphttp-headerscontent-typehttp-request or ask your own question.

sohogenerous – 2019