Jonathan Holvey
Jonathan Holvey

Reputation: 707

Are multiple status code headers allowed in a single HTTP response?

I have a web app which sends HTTP status codes. In some instances, a 404 code is sent as a header, but then a 200 error is sent as well, in the same response.

HTTP/1.0 404 Not Found
HTTP/1.0 200 Ok

I can't change the execution order to prevent the first status code from being sent, so the second code is attempting to override it.

Chrome ignores the first code and assumes the status to be Ok. Does this conform to the HTTP standard, and should I rely on it?

Upvotes: 4

Views: 5401

Answers (2)

Julian Reschke
Julian Reschke

Reputation: 41997

No, it does not conform to the standard, and you should not rely on it. See https://www.greenbytes.de/tech/webdav/rfc7230.html#rfc.section.5.6:

More than one response message per request only occurs when one or more informational responses (1xx, see Section 6.2 of [RFC7231]) precede a final answer to the same request.

Upvotes: 6

DewiW
DewiW

Reputation: 148

The rfc for http 1.1 is given here: https://www.rfc-editor.org/rfc/rfc7230

Section 2.1 states:

A server responds to a client's request by sending one or more HTTP response messages, each beginning with a status line ...

The standard states that you can send more than one response, if you wish, but that each response must have it's own status line. Further, the first line of the header must contain the status-line/code.

So, according to the standard interpreted literally, in theory you can send more than one response, but I've no idea what browsers would do with that, and definitely wouldn't rely on it.

What you've got at the moment is conforming to the rfc; the rfc doesn't say you can't have more than status line, only that the status line on the first line of each response is the one that matters - which chrome doesn't interpret correctly according to the rfc.

It might work, but I wouldn't rely on it.

Upvotes: 0

Related Questions