Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the HTTP Protocol support multiple content types in response headers?

I'm wondering if I can return the response header

Content-Type: application/json text/json  

or isn't this legal?

The HTTP protocol states that Content-Type should be of mediaType and that mediaType is defined as the following:

   media-type     = type "/" subtype *( ";" parameter )    type           = token    subtype        = token 

But I'm having trouble interpreting what this means, can anyone enlighten me?

Thanks!

like image 830
netbrain Avatar asked Apr 27 '11 18:04

netbrain


People also ask

Can a HTTP request have multiple headers?

The HTTP Headers can have one or more values depending on the header field definitions. A multi-valued header will have comma separated values.

Can you have multiple content types?

By defining multiple content types for a list or library, you can make it easy to build libraries or lists that store different types of documents together in one place. For example, a library can contain both the documents and the graphics related to a project.

How many types of HTTP headers are there?

There are four types of HTTP message headers: General-header: These header fields have general applicability for both request and response messages. Client Request-header: These header fields have applicability only for request messages.

What does HTTP response header contain?

An HTTP response header includes information in a text-record form that a Web server transmits back to the client's browser. The response header contains particulars such as the type, date and size of the file sent back by the server, as well as information regarding the server.


2 Answers

You need to look at the definition of the header field:

http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.14.17

Content-Type = "Content-Type" ":" media-type

so it takes a single media-type, which is defined by the grammar you quoted above.

So the answer is: a single type/subtype, followed by optional parameters.

like image 130
Julian Reschke Avatar answered Sep 24 '22 03:09

Julian Reschke


No. As pointed out in the accepted answer, the header value itself does not allow for multiple values, and it is also not allowed to send multiple Content-Type headers:

A sender MUST NOT generate multiple header fields with the same field name in a message unless either the entire field value for that header field is defined as a comma-separated list [i.e., #(values)] or the header field is a well-known exception (as noted below).

https://www.rfc-editor.org/rfc/rfc7230#section-3.2.2

None of the "escape clauses" holds, because media-type does not allow a comma-separated list of values, and Content-Type evidently is not a well-known exception, either.

like image 23
bkgs Avatar answered Sep 20 '22 03:09

bkgs