HTTP Protocol
Also HTTP · Media Type · MIME Type
HTTP (Hypertext Transfer Protocol) is the text-based request–response protocol between browser (client) and web server, in which a request consists of a request line, headers and an optional body, and a response of a status line, headers (e.g. Content-Length, Content-Type), an empty line and the content.
Know these firstNetwork Socket
Formula#
Request format#
METHOD /path?param=value HTTP/1.1
Host: hostname:port
<more headers>
<body, only for e.g. POST>
/path?param=value is the part of the URL after http://<host>:<port>.
GET request example:
GET /search?query=bla HTTP/1.1
Host: localhost:8000
POST request example:
POST /submit HTTP/1.1
Host: localhost:8000
Content-Type: application/x-www-form-urlencoded
Content-Length: 9
query=bla
Response format#
HTTP/1.1 <status-code> <reason>
Content-Type: <media type>
Content-Length: <number of content bytes>
<content>
Example:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 18
{"result": "data"}
Status codes#
| Code | Meaning |
|---|---|
| 200 | OK |
| 301 | Moved Permanently |
| 400 | Bad Request |
| 403 | Forbidden: the resource exists but may not be accessed by this client |
| 404 | Not Found: the requested resource does not exist |
| 500 | Internal Server Error |
Media Types#
A media type (aka content type, formerly MIME type, MIME = Multipurpose Internet Mail Extensions) is a standard name for a type of content sent across the internet. The server declares it in the Content-Type header, optionally with the charset (text/html; charset=utf-8).
| Media type | Used for |
|---|---|
text/html | HTML pages |
text/css | CSS stylesheets |
text/plain | plain text |
application/javascript | JavaScript |
application/json | JSON data |
image/jpeg | JPG image |
image/x-icon | ICO image (notably favicon.ico) |
application/x-www-form-urlencoded | form POST data |
Properties#
- HTTP/1 demands that line breaks are
\r\n; the recommendation is to accept a single\n, too. Content-Lengthcounts bytes of the content, not characters (Character Encoding).- HTTP is stateless: each request is independent.
GETparameters are in the URL (visible, bookmarkable, limited length) and must be URL-encoded.POSTsends arbitrarily large data in the body along with the URL. There are many more request types and headers.- Connection reuse: browsers send
Connection: keep-aliveto reuse the connection; a server answeringConnection: closeforces a new connection per request (HTTP Server > Concurrency). - Cross-origin headers:
Origin(request) andAccess-Control-Allow-Origin(response) implement CORS (Same-Origin Policy). - APIs use the same format: the LLM and embedding APIs of Sheet 12 are
POSTrequests withContent-Type: application/json, anAuthorization: Bearer <key>header and a JSON body. - Debugging: browser development console (F12), tab Network, shows all requests and responses;
curl -vprints them on the command line.
Example#
Sources#
- Slides 09, p. 11 (request, response,
\r\n), p. 12 (POST, 403, 404), p. 13 (media types), p. 14 (console), p. 21 (Connection) - DBIS_VL09 > HTTP, DBIS_VL09 > Request types and status codes, DBIS_VL09 > Media types, DBIS_VL09 > Reusing connections, DBIS_VL10 > CORS, DBIS_VL12 > Using an LLM via API (ES12)
- DBIS Exam WS1920 (not published) T3.3, DBIS Exam WS2324 (not published) T4.3, DBIS Exam WS1516 (not published) T3.2, DBIS Exam WS1819 (not published) T4.2