Understanding HTTP
HTTP, the Hypertext Transfer Protocol, defines how a client and server exchange requests and responses. Your Python program is the client: it requests a resource identified by a URL. The server responds with a status code, headers describing the response, and usually a body containing data such as HTML or JSON.
Read the Status Code
A status code describes the outcome of a request:
| Code | Meaning |
|---|---|
| 200 OK | The request succeeded. |
| 404 Not Found | The server could not find the requested resource. |
| 429 Too Many Requests | The client has exceeded a request limit. |
| 500 Internal Server Error | The server encountered an unexpected problem. |
These codes help your program decide whether to process the response or handle a failure. A successful response still needs to contain the data your program expects.
Make a GET Request
A GET request asks for a resource without requesting a change to it. The third-party requests package provides get() for this purpose. Install it in your active environment with python -m pip install requests if needed.
This complete example uses JSONPlaceholder, a service providing sample data for practice [1]. It requires an internet connection.
import requests
url = "https://jsonplaceholder.typicode.com/posts/1"
response = requests.get(url, timeout=10)
response.raise_for_status()
print("Status code:", response.status_code)
print("Content type:", response.headers.get("Content-Type"))Expected output — requires a successful live request
Status code: 200
Content type: application/json; charset=utf-8
get() returns a response object. timeout=10 limits how long Requests waits for a connection or incoming data; it is not a fixed limit on the whole download. raise_for_status() raises an exception for an HTTP error, so the program does not process an error page as valid data. Connection failures and timeouts also raise exceptions. The two print calls then show the status and the body’s declared format; a server can change its headers.
We can now fetch a response. After looking at HTML structure, we will extract useful values from JSON and HTML rather than printing their raw contents.
Reference
- JSONPlaceholder. Guide.