We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Response

While there isn't an official list of all the headers that should be in most responses, there are a few that are common enough that we should include them. Namely:

So, let's build some more of the pieces we'll need to send valid dynamic responses. Remember that HTTP responses follow the same HTTP message format:

 HTTP-message   = start-line CRLF
                  *( field-line CRLF )
                  CRLF
                  [ message-body ]

The only difference is that the start-line is a status-line instead of a request-line. From RFC 9112:

status-line = HTTP-version SP status-code SP [ reason-phrase ]

For example:

HTTP/1.1 200 OK

Or perhaps:

HTTP/1.1 404 Not Found

Some interesting tidbits about the reason-phrase, from Section 4,

A client SHOULD ignore the reason-phrase content because it is not a reliable channel for information (it might be translated for a given locale, overwritten by intermediaries, or discarded when the message is forwarded via other versions of HTTP). A server MUST send the space that separates the status-code from the reason-phrase even when the reason-phrase is absent (i.e., the status-line would end with the space).

So, while reason phrases are typically included (and match one to one with the status code), they are not required and should be ignored by clients. Kinda funny.

Assignment

    • 200
    • 400
    • 500
    • 200 should return HTTP/1.1 200 OK
    • 400 should return HTTP/1.1 400 Bad Request
    • 500 should return HTTP/1.1 500 Internal Server Error
    • Any other code should just leave the reason phrase blank.
  1. HTTP/1.1 200 OK
    Content-Length: 0
    Connection: close
    Content-Type: text/plain
    

    To keep things simple for this course, we'll only use the mimetype, e.g. text/plain in the Content-Type header. Don't add a charset or you'll fail the exact-match tests.

Run and submit the CLI tests with your server running.