Back
+15 XP
Concept

HTTP Without Libraries: Raw HTTP GET Requests

HTTP/1.1 is plain text over a TCP stream. A GET request is formatted as `b'GET /path HTTP/1.1\r\nHost: example.com\r\n\r\n'`. Knowing this demystifies all web traffic 🌐.

python
# An HTTP request is just structured bytes over a socket:
request = (
    b"GET / HTTP/1.1\r\n"
    b"Host: example.com\r\n"
    b"Connection: close\r\n"
    b"\r\n"  # Empty line marks end of headers! 🌐
)

print(request.decode("ascii"))

# 75. HTTP Without Libraries: Raw HTTP GET Requests HTTP/1.1 is plain text over a TCP stream. A GET request is formatted as `b'GET /path HTTP/1.1\r\nHost: example.com\r\n\r\n'`. Knowing this demystifies all web traffic 🌐. ```python # An HTTP request is just structured bytes over a socket: request = ( b"GET / HTTP/1.1\r\n" b"Host: example.com\r\n" b"Connection: close\r\n" b"\r\n" # Empty line marks end of headers! 🌐 ) print(request.decode("ascii")) ``` The `\r\n\r\n` sequence (CRLF + CRLF) is the protocol delimiter that tells web servers you are done sending headers and ready for the response! 🌐 --- ### Question What character sequence marks the end of HTTP request headers in HTTP/1.1? 🧠 - [ ] a. `\r\n\r\n` (carriage return + newline repeated twice) 🌐 - [ ] b. `<EOF>` - [ ] c. `\0\0` (null bytes) - [ ] d. `STOP_HEADERS` --- ### Challenge Write a function `build_get(host, path)` returning `f'GET {path} HTTP/1.1\r\nHost: {host}\r\n\r\n'`. Print `build_get('xicae.com', '/status')`. 🎯 <details> <summary>Solution</summary> ```python def build_get(host, path): return f"GET {path} HTTP/1.1\r\nHost: {host}\r\n\r\n" print(build_get("xicae.com", "/status")) ``` </details>

Checkpoint

What character sequence marks the end of HTTP request headers in HTTP/1.1? 🧠

Practice

Write a function `build_get(host, path)` returning `f'GET {path} HTTP/1.1\r\nHost: {host}\r\n\r\n'`. Print `build_get('xicae.com', '/status')`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks