Set more than one HTTP header with the same name?
As you're looking for use-cases, maybe Accept
would be a valid one.
- Accept: application/json
- Accept: application/xml
Since duplicate headers can cause issues with various web-servers and APIs (regardless of what the spec says), I doubt there is any general purpose use case where this is best practice. That's not to say someone somewhere isn't doing it, of course.
It's only allowed for headers using a very specific format, see RFC 2616, Section 4.2.
It's commonly used for Set-Cookie:
. Many servers set more than one cookie.
Of course, you can always set them all in a single header.
Actually, I think you cannot set multiple cookies in one header. So that's a necessary use-case.
The Cookie spec (RFC 2109) does claim that you can combine multiple cookies in one header the same way other headers can be combined (comma-separated), but it also points out that non-conforming syntaxes (like the Expires
parameter, which has ,
s in its value) are still common and must be dealt with by implementations.
So, if you use Expires
params in your Set-Cookie
headers and you don't want all your cookies to expire at the same time, you probably need to use multiple headers.
Update: Evolution of the Cookie spec
RFC 2109 has been obsoleted by RFC 2965 that in turn got obsoleted by RFC 6265, which is stricter on the issue:
Origin servers SHOULD NOT fold multiple
Set-Cookie
header fields into a single header field. The usual mechanism for folding HTTP headers fields (i.e., as defined in [RFC2616]) might change the semantics of theSet-Cookie
header field because the%x2C
(",") character is used bySet-Cookie
in a way that conflicts with such folding.
Side note
RFC 6265 uses the verb "folding" when it refers to combining multiple header fields into one, which is ambiguous in the context of the HTTP/1 specs (both by RFC2616, and its successor, RFC 7230) where:
"folding" consistently refers to line folding, and
the verb "combine" is used to describe merging same headers.
Combining header fields:
See RFC 2616, Section 4.2, Message Headers (quoted in the question), but searching for the for the word "combine" will bring up special cases.
The above item obsoleted by RFC 7230, Section 3.2.2, Field Order:
A recipient MAY combine multiple header fields with the same field name into one
field-name: field-value
pair, without changing the semantics of the message, by appending each subsequent field value to the combined field value in order, separated by a comma. The order in which header fields with the same field name are received is therefore significant to the interpretation of the combined field value; a proxy MUST NOT change the order of these field values when forwarding a message.Note: In practice, the "Set-Cookie" header field (RFC6265) often appears multiple times in a response message and does not use the list syntax, violating the above requirements on multiple header fields with the same name. Since it cannot be combined into a single field-value, recipients ought to handle
Set-Cookie
as a special case while processing header fields. (See Appendix A.2.3 of [Kri2001] for details.)
Line folding:
From RFC 2616, Section 2.2, Basic Rules:
HTTP/1.1 header field values can be folded onto multiple lines if the continuation line begins with a space or horizontal tab. All linear white space, including folding, has the same semantics as SP. A recipient MAY replace any linear white space with a single SP before interpreting the field value or forwarding the message downstream.
The above section obsoleted by RFC 7230, Section 3.2.4, Field Parsing:
Historically, HTTP header field values could be extended over multiple lines by preceding each extra line with at least one space or horizontal tab (
obs-fold
). This specification deprecates such line folding except within themessage/http
media type (Section 8.3.1). A sender MUST NOT generate a message that includes line folding (i.e., that has any field-value that contains a match to theobs-fold
rule) unless the message is intended for packaging within themessage/http
media type.A server that receives an
obs-fold
in a request message that is not within amessage/http
container MUST either reject the message by sending a 400 (Bad Request
), preferably with a representation explaining that obsolete line folding is unacceptable, or replace each receivedobs-fold
with one or more SP octets prior to interpreting the field value or forwarding the message downstream.A proxy or gateway that receives an
obs-fold
in a response message that is not within a message/http container MUST either discard the message and replace it with a 502 (Bad Gateway
) response, preferably with a representation explaining that unacceptable line folding was received, or replace each received obs-fold with one or more SP octets prior to interpreting the field value or forwarding the message downstream.A user agent that receives an
obs-fold
in a response message that is not within amessage/http
container MUST replace each receivedobs-fold
with one or more SP octets prior to interpreting the field value.