4. httpbin.org, your API playground
When you're learning APIs, it helps to have a safe practice server that always behaves predictably. httpbin.org is exactly that: a free service built specifically for experimenting with HTTP.
You can send httpbin requests of all kinds, and it'll simply show you, in plain JSON, what it received and how it interpreted your request. Developers treat it as a Swiss Army knife for HTTP: it's where you go to:
- See exactly what headers your client is sending
- Inspect query parameters and request metadata
- Understand what cookies look like in requests and responses
- Test timeouts and slow responses with
/delay - Feel what different status codes look like in your code
Because it's simple and predictable, and needs no account or API key, it's perfect for experimenting and debugging without worrying about breaking anything. In the rest of this chapter, you'll use httpbin as your practice server while you build up real confidence with requests and responses.
Endpoints you'll use
httpbin.org/get, returns the details of a GET request (headers, query parameters, URL)httpbin.org/status/404, returns a custom status code (like404 Not Found)httpbin.org/json, returns sample JSON datahttpbin.org/delay/3, waits 3 seconds before responding (useful for testing timeouts)httpbin.org/image/png, returns an image you can download or save to a file
Your first httpbin request
The /get endpoint simply echoes back everything it received: your headers, your origin IP, the final URL. It's like holding up a mirror to your HTTP request so you can see it from the server's point of view.
Save this as httpbin_get.py at the project root:
import requests
url = "https://httpbin.org/get"
response = requests.get(url, timeout=5)
print("Status:", response.status_code)
print("Body:", response.text)
Run it from the project root:
$ python httpbin_get.py
Status: 200
Body: {
"args": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "python-requests/X.Y.Z"
},
"origin": "203.0.113.45",
"url": "https://httpbin.org/get"
}
What the server saw
httpbin just shows you what it received. Look at what's in that response:
args, any query parameters you sent in the URL (empty, since you didn't send any).headers, extra information your client included automatically, like what software it's running.origin, the IP address the request came from.url, the exact URL that got requested.
Notice the User-Agent header. Your client automatically told the server which library it's running, in this case python-requests with whatever version you have installed. Every browser and HTTP library sends its own user-agent string. Servers log these for analytics, debugging, or sometimes to adjust how they respond to different clients.
Next, we'll look at one of those automatic behaviours that isn't turned on by default: the request timeout. Without one, a single slow server can freeze your entire program.