API (Application Programming Interface) 🌐

An API is a set of rules that allows different software systems to communicate. It enables data exchange and functionality between systems, often hosted on different servers or platforms.

Key Components of an API Request πŸ”‘

  1. Endpoint πŸ“: The URL where an API can be accessed (e.g., https://api.weather.com/forecast).
  2. HTTP Method πŸ”„: The action to be performed:
    • GET: Retrieve data πŸ“₯
    • POST: Send data πŸ“€
    • PUT: Update data ✏️
    • DELETE: Remove data ❌
  3. Headers 🧳: Additional information like authentication tokens and content type.
  4. Parameters/Payload πŸ“¦: Data sent with the request, either in the URL (for GET) or body (for POST/PUT).
  5. Authentication πŸ”‘: Required for some APIs, typically via API keys or tokens.
  6. Response πŸ“¬: The server’s reply, including a status code (e.g., 200 OK, 404 Not Found) and data (usually in JSON or XML format).
  7. Parsing the Response 🧩: Extracting relevant data from the response (e.g., JSON).

Hands-On πŸ› οΈ

To interact with RESTful APIs in Python, use libraries like requests. Here’s a basic example of making an API request using requests.

1
2
3
4
5
6
7
8
9
10
11
import requests

response = requests.get("https://api.weather.com/forecast")
data = response.json()  # Parse JSON response
print(data)


### Installing the requests library:

```bash
pip install requests

Making GET Requests:

1
2
3
4
5
6
7
8
9
10
11
12
import requests

# Make a GET request to a URL
response = requests.get('https://jsonplaceholder.typicode.com/posts/1')

# Check if the request was successful (HTTP status code 200)
if response.status_code == 200:
    # Print the response content
    print(response.json())
else:
    # Print an error message if the request was not successful
    print(f'Error: {response.status_code}')

In this example, we’re making a GET request to retrieve a specific post from the JSONPlaceholder API. We then check if the request was successful (status code 200) and print the response content.

Making POST Requests:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import requests

# Define the URL and payload data
url = 'https://jsonplaceholder.typicode.com/posts'
data = {
    'title': 'foo',
    'body': 'bar',
    'userId': 1
}

# Make a POST request with the data
response = requests.post(url, json=data)

# Check if the request was successful (HTTP status code 201 for created)
if response.status_code == 201:
    print(f'Success! New post created with ID {response.json()["id"]}')
else:
    print(f'Error: {response.status_code}')

In this example, we’re making a POST request to create a new post on the JSONPlaceholder API. We provide the URL and the data we want to send as a JSON payload.

Making PUT and DELETE Requests:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import requests

# Define the URL and updated data
url = 'https://jsonplaceholder.typicode.com/posts/1'
data = {
    'title': 'updated title',
    'body': 'updated body',
}

# Make a PUT request to update the resource
response = requests.put(url, json=data)

# Check if the request was successful (HTTP status code 200 for OK)
if response.status_code == 200:
    print(f'Success! Post updated: {response.json()}')
else:
    print(f'Error: {response.status_code}')

# Make a DELETE request to delete the resource
response = requests.delete(url)

# Check if the request was successful (HTTP status code 200 for OK)
if response.status_code == 200:
    print('Success! Post deleted.')
else:
    print(f'Error: {response.status_code}')

In the above example, we’re making a PUT request to update a specific post and a DELETE request to delete the same post.

These are basic examples to get you started with making HTTP requests and interacting with RESTful APIs in Python. Depending on the API you’re working with, you may need to handle authentication, headers, pagination, and other features specific to that API.