Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Explore Secret HTTP Requests in Python

Master secret Python HTTP requests to connect with APIs and manage web data.

8 min readNov 20, 2024

--

Press enter or click to view image in full size
Image generated by me , feel free to join my LinkedIn group here

Understanding HTTP requests is a crucial skill for any Python backend developer. A firm grasp on these ideas allows for effective communication between apps and servers, a fundamental element of web development. By learning how to manage HTTP requests, you will be more prepared to interact with APIs and oversee data exchanges in your projects.

Hi, my name is CyCoderX and in this article we will explore the details of creating HTTP requests with Python. We will help you set up and guide you on fetching data from a remote server, sending data to an API, or managing web interactions. We will show you how to make GET and POST requests and cover advanced topics such as handling responses and setting custom headers.

Let’s dive in!

I write articles for everyone to enjoy, and I’d love your support by following me for more Python, SQL, Data Engineering and Data Science content.😊

Understanding HTTP Requests:

Definition: HTTP (Hypertext Transfer Protocol) requests are the foundation of data communication on the web.

Methods: Common HTTP request methods include:

  • GET: Retrieves data from the server.
  • POST: Sends data to the server to create or update resources.
  • PUT: Updates existing resources on the server.
  • DELETE: Removes resources from the server.
  • PATCH: Partially updates resources on the server.

Structure: An HTTP request consists of:

  • Request Line: Indicates the method, URL, and HTTP version.
  • Headers: Provide metadata such as content type, user agent, and authorization.
  • Body: Contains data to be sent with POST, PUT, or PATCH requests (often in JSON or form-data format).

Status Codes: The server responds with status codes to indicate the result of the request:

  • 2xx: Success (e.g., 200 OK, 201 Created)
  • 3xx: Redirection (e.g., 301 Moved Permanently, 302 Found)
  • 4xx: Client errors (e.g., 400 Bad Request, 404 Not Found)
  • 5xx: Server errors (e.g., 500 Internal Server Error, 503 Service Unavailable)

Headers: Key headers include:

  • Content-Type: Indicates the media type of the request/response body (e.g., application/json).
  • Authorization: Contains credentials for authentication.
  • Accept: Specifies the media types acceptable for the response.

Cookies: Small pieces of data sent by the server to be stored by the client, often used for session management.

URL Parameters: Data sent in the URL, often used with GET requests to filter or specify data (e.g., ?id=123).

Payload: Data sent in the body of the request, used with POST, PUT, and PATCH methods.

Setting Up the Requests Library
To get started, ensure you have the requests library installed. I personally use this library and would recommend it. Although it’s not part of Python’s standard library, it’s widely used and can be easily installed via pip:

pip install requests

Once installed, import it into your script:

import requests

Making GET and POST Requests

Making a Basic GET Request

The GET method is used to retrieve data from a specified resource. With requests, a GET request is straightforward:

response = requests.get('https://jsonplaceholder.typicode.com/posts')
print(response.status_code) # HTTP status code
print(response.text) # Response body as text

In this code:

  • response.status_code provides the HTTP status, such as 200 for success.
  • response.text outputs the body of the response.

Handling Query Parameters in GET Requests

For GET requests with parameters, use the params argument. Here’s how:

params = {'userId': 1}
response = requests.get('https://jsonplaceholder.typicode.com/posts', params=params)
print(response.url) # Shows the full URL with query parameters
print(response.json()) # Outputs JSON data

Making a POST Request

POST requests are used to send data to a server. For example, submitting form data or adding a new resource to an API. Here’s how to make a POST request:

data = {'title': 'New Post', 'body': 'This is the content', 'userId': 1}
response = requests.post('https://jsonplaceholder.typicode.com/posts', json=data)
print(response.status_code)
print(response.json()) # Outputs the server's response in JSON format

In the example above, json=data sends the data as a JSON payload. For most APIs, JSON is the preferred format, but there are other formats that can be used.

Press enter or click to view image in full size
Photo by Lauren Mancke on Unsplash

Handling Responses

Inspecting the Response Object
When you make an HTTP request using requests, the response from the server is stored in a Response object. This object includes useful information such as status codes, headers, and content.

response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
print(response.status_code) # Status code (e.g., 200, 404)
print(response.headers) # Response headers
print(response.content) # Raw response content in bytes
print(response.json()) # Parsed JSON content if response is JSON

Key response attributes include:

  • status_code: checks if the request was successful.
  • headers: provides metadata about the response, such as content type and server.
  • json(): automatically parses the JSON response, which is especially useful when working with RESTful APIs.

Error Handling with Status Codes
It’s good practice to handle responses based on status codes. The raise_for_status() method raises an error if the request was unsuccessful:

try:
response = requests.get('https://jsonplaceholder.typicode.com/posts/invalid')
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")

Using raise_for_status() helps manage error handling effectively, ensuring that your application reacts appropriately to various server responses, such as 404 Not Found or 500 Internal Server Error.

Setting Request Headers

In many cases, you’ll need to customize request headers to include information like authentication tokens, user agents, or content types. Use the headers parameter to set custom headers:

headers = {'User-Agent': 'my-app/1.0', 'Authorization': 'Bearer your_token_here'}
response = requests.get('https://api.example.com/data', headers=headers)
print(response.status_code)

Custom headers are essential when interacting with APIs that require authentication or expect specific header values. Always refer to the API documentation to understand the required headers.

Get CyCoderX’s stories in your inbox

Join Medium for free to get updates from this writer.

Commonly Used Headers
Some frequently used headers include:

  • Content-Type: specifies the data format, such as application/json or text/plain.
  • Authorization: often used to provide authentication tokens.
  • User-Agent: identifies the client software (optional but commonly used).

Using headers allows you to ensure that requests comply with the requirements of the server, facilitating a smoother interaction with web services.

Creating a Helper Function with a Base URL

Using a Base URL with Dynamic Parameters
When working with APIs, you’ll often need to make multiple requests to the same base URL with different parameters. Creating a helper function to handle this can make your code modular and easier to manage.

Here’s an example function that uses a base URL, accepts a dictionary of parameters, and allows for a flexible setup:

import requests

def fetch_data(base_url, endpoint, params=None, headers=None):
"""
Fetch data from the given API endpoint with optional parameters.

Parameters:
- base_url (str): The root URL for the API.
- endpoint (str): The specific API endpoint.
- params (dict, optional): Dictionary of URL parameters.
- headers (dict, optional): Dictionary of headers.

Returns:
- dict: JSON response from the API if successful.
- None: If an error occurs.
"""
url = f"{base_url}/{endpoint}"
try:
response = requests.get(url, params=params, headers=headers)
response.raise_for_status() # Raise an error for unsuccessful requests
return response.json() # Return the parsed JSON response
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None

Using the Helper Function
This function simplifies requests by allowing you to specify the base URL and change only the endpoint and parameters as needed. Here’s an example of how to use it:

base_url = 'https://jsonplaceholder.typicode.com'
endpoint = 'posts'
params = {'userId': 1}

data = fetch_data(base_url, endpoint, params=params)
if data:
for item in data:
print(item)

In this example:

  • The base URL points to the root of the API.
  • The endpoint and params specify the request’s exact target and parameters, making it easy to adjust requests without changing the main function.

This approach modularizes your API calls, keeping the code organized and scalable, especially in applications that require many requests to the same base URL.

Free APIs to Practice Your HTTP Requests Skills

Here are some free and open APIs that you can use to practice making HTTP requests in Python. Each of these APIs provides unique data and endpoints, allowing you to apply the methods covered in this article.

1. JSONPlaceholder

URL: https://jsonplaceholder.typicode.com
JSONPlaceholder is a commonly used API for testing and prototyping. It provides fake data on posts, comments, users, and more, which is ideal for experimenting with GET and POST requests.

Example Endpoints:

  • /posts - Retrieve posts
  • /users - Retrieve user data

Example:

requests.get("https://jsonplaceholder.typicode.com/posts")

2. OpenWeatherMap

URL: https://api.openweathermap.org/data/2.5
OpenWeatherMap provides real-time weather data for any city. You’ll need a free API key to use it, but it’s a great resource to practice handling query parameters.

Example Endpoints:

  • /weather - Current weather data
  • /forecast - 5-day weather forecast

Example:

params = {'q': 'London', 'appid': 'your_api_key'}
requests.get("https://api.openweathermap.org/data/2.5/weather", params=params)

3. The Dog API

URL: https://api.thedogapi.com/v1
The Dog API provides images and data on dog breeds. It’s free to use and great for testing image data retrieval and working with JSON responses that contain nested information.

Example Endpoints:

  • /breeds - List of dog breeds
  • /images/search - Random dog images

Example:

requests.get("https://api.thedogapi.com/v1/images/search")

4. SpaceX API

URL: https://api.spacexdata.com/v4
The SpaceX API provides public data on launches, rockets, missions, and more. This is a good choice for practicing with JSON data in the context of a real-world application.

Example Endpoints:

  • /launches - SpaceX launches
  • /rockets - Information on SpaceX rockets

Example:

requests.get("https://api.spacexdata.com/v4/launches")

5. CoinGecko API

URL: https://api.coingecko.com/api/v3
CoinGecko offers cryptocurrency data, including prices, market caps, and more. This is useful for practicing HTTP requests that handle financial data and parameters like currency or market data.

Example Endpoints:

  • /simple/price - Current price of specific cryptocurrencies
  • /coins/markets - Detailed market data

Example:

params = {'ids': 'bitcoin', 'vs_currency': 'usd'}
requests.get("https://api.coingecko.com/api/v3/simple/price", params=params)

These APIs offer diverse data and endpoints, allowing you to practice with different types of requests, parameters, and responses. Be sure to check each API’s documentation for usage limits or authentication requirements.

In conclusion

The Python requests library offers effective features for managing HTTP requests effortlessly. Having knowledge of creating GET and POST requests, managing responses, defining headers, and developing reusable functions allows you to effectively communicate with APIs and various online assets.

Python developers must have a core skill in making HTTP requests as it is crucial for developing applications depending on external data. Equipped with these methods, you are ready to create dependable and effective API requests for your projects.

Happy Coding!

Final Words:

Thank you for taking the time to read my article. Article first published on Medium by CyCoderX.

Hi, I’m CyCoderX! An engineer passionate about sharing knowledge, I write articles about Python, SQL, Data Science, Data Engineering and more!

Please consider supporting me by:

  1. Clapping 50 times for this story
  2. Leaving a comment telling me your thoughts
  3. Highlighting your favorite part of the story
  4. Connecting with me on LinkedIn and joining my LinkedIn Group

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--

CyCoderX
CyCoderX

Written by CyCoderX

Machine Learning & Data Engineer | Data Science | Python & SQL | AI | Software Developer | Azure & AWS Cloud Specialist | Blogger simplifying Big Data & Trends

Python in Plain English
Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.