python Logorequests

The `requests` library is an elegant and simple HTTP library for Python, designed to make HTTP requests as human-friendly as possible. It abstracts the complexities of making requests behind a beautifully simple API, allowing developers to interact with web services with minimal effort. Unlike Python's built-in `http.client` or `urllib`, `requests` provides a much higher-level interface that is both intuitive and powerful.

Key features and functionalities include:

- Diverse HTTP Methods: Easily perform `GET`, `POST`, `PUT`, `DELETE`, `HEAD`, and `OPTIONS` requests.
- Sending Data: Supports various ways to send data:
- Query parameters for `GET` requests using the `params` argument (e.g., `requests.get('url', params={'key': 'value'})`).
- Form-encoded data for `POST` requests using the `data` argument (e.g., `requests.post('url', data={'key': 'value'})`).
- JSON data for `POST` or `PUT` requests using the `json` argument (e.g., `requests.post('url', json={'key': 'value'})`), which automatically sets the `Content-Type` header to `application/json`.
- Custom Headers: Easily add custom HTTP headers to requests using the `headers` argument.
- Authentication: Built-in support for various authentication schemes, including Basic Authentication.
- Timeouts: Specify a timeout for requests to prevent them from hanging indefinitely.
- Sessions: Use `Session` objects to persist certain parameters across requests (like cookies, headers, and authentication) for more efficient interaction with APIs.
- File Uploads: Simple API for uploading files.
- Response Handling: The `Response` object returned by `requests` methods provides convenient attributes and methods to access various parts of the response:
- `response.status_code`: The HTTP status code (e.g., 200, 404).
- `response.text`: The response content as a Unicode string.
- `response.json()`: Parses the response content as JSON (if applicable).
- `response.content`: The response content as bytes.
- `response.headers`: A dictionary-like object of response headers.
- `response.url`: The final URL of the request.
- `response.raise_for_status()`: Raises an `HTTPError` for bad responses (4xx or 5xx status codes).
- Error Handling: Simplifies checking for and handling non-200 status codes.

The `requests` library is an indispensable tool for any Python developer needing to interact with web services, APIs, or scrape websites due to its robustness, ease of use, and comprehensive features.

Example Code

import requests
import json  Not strictly needed for requests.json(), but good for pretty printing

 --- 1. Simple GET Request ---
print("--- Simple GET Request ---")
try:
    response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
    response.raise_for_status()  Raise an exception for HTTP errors (4xx or 5xx)
    print(f"Status Code: {response.status_code}")
    print(f"Response URL: {response.url}")
    print("Response Content (first 100 chars):")
    print(response.text[:100])
    print("\n")
except requests.exceptions.HTTPError as err:
    print(f"HTTP error occurred: {err}")
except requests.exceptions.ConnectionError as err:
    print(f"Connection error occurred: {err}")
except requests.exceptions.Timeout as err:
    print(f"Timeout error occurred: {err}")
except requests.exceptions.RequestException as err:
    print(f"An unexpected error occurred: {err}")


 --- 2. GET Request with Query Parameters ---
print("--- GET Request with Query Parameters ---")
params = {'userId': 1, 'id': 2}
try:
    response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params)
    response.raise_for_status()
    print(f"Status Code: {response.status_code}")
    print(f"Response URL (with params): {response.url}")
    print("Filtered Posts:")
     requests.json() automatically parses JSON into a Python dictionary/list
    print(json.dumps(response.json(), indent=2))
    print("\n")
except requests.exceptions.RequestException as err:
    print(f"An error occurred: {err}")


 --- 3. POST Request with JSON Data and Custom Headers ---
print("--- POST Request with JSON Data and Custom Headers ---")
new_post_data = {
    'title': 'foo',
    'body': 'bar',
    'userId': 1
}
headers = {
    'Accept': 'application/json'  We can specify what type of response we prefer
}

try:
     The 'json' argument automatically sets 'Content-Type: application/json' header
    response = requests.post(
        'https://jsonplaceholder.typicode.com/posts',
        json=new_post_data,
        headers=headers
    )
    response.raise_for_status()  Expecting a 201 Created status
    print(f"Status Code: {response.status_code}")
    print("New Post Created:")
    print(json.dumps(response.json(), indent=2))
    print("\n")
except requests.exceptions.RequestException as err:
    print(f"An error occurred: {err}")


 --- 4. Using Sessions for Persistent Parameters (e.g., headers, cookies) ---
print("--- Using Sessions ---")
with requests.Session() as session:
     Headers set on the session will be sent with all requests made using this session
    session.headers.update({
        'X-Custom-App-ID': 'MyPythonApp',
        'Accept': 'application/json'
    })
    
    try:
         Request 1: Get user with ID 1
        user_response = session.get('https://jsonplaceholder.typicode.com/users/1')
        user_response.raise_for_status()
        print(f"Session Request 1 Status Code: {user_response.status_code}")
        print("User 1 Details (from session):")
        print(json.dumps(user_response.json(), indent=2))
        
         Request 2: Get posts by user 1 (session headers are reused, and connection might be pooled)
        posts_response = session.get('https://jsonplaceholder.typicode.com/posts', params={'userId': 1})
        posts_response.raise_for_status()
        print(f"\nSession Request 2 Status Code: {posts_response.status_code}")
        print(f"Number of posts by user 1: {len(posts_response.json())}")

    except requests.exceptions.RequestException as err:
        print(f"An error occurred during session usage: {err}")