Lesson 25 of 30
In Progress

Consuming REST APIs

REST (Representational State Transfer) APIs are a common way for applications to communicate with each other over the web. They allow different systems to exchange data and trigger actions by making HTTP requests to a specific endpoint (URL).

GET Request

In Python, consuming a REST API is very easy thanks to the “requests” library. Here is an example of making a GET request to a REST API and printing the response:

import requests

url = "https://api.example.com/endpoint"

response = requests.get(url)

print(response.text)

In the above example, we first import the “requests” library and then use the “get()” function to make a GET request to the specified URL. The response is stored in a “Response” object, which has various properties such as the status code, headers, and the actual response data (in this case, stored as text).

POST, PUT and DELETE Reqests

To make other types of HTTP requests (such as POST, PUT, DELETE, etc.), we can use the corresponding functions in the “requests” library:

import requests

url = "https://api.example.com/endpoint"

# Make a POST request
response = requests.post(url, data={"key": "value"})

# Make a PUT request
response = requests.put(url, data={"key": "value"})

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

In the above examples, we pass in a “data” argument to send data along with the request. We can also pass in other parameters such as “headers” and “params” to customize the request further.

GET Request with an API Key

Here is an example of making a GET request to a REST API that requires an API key in the header:

import requests

url = "https://api.example.com/endpoint"
headers = {"Authorization": "Bearer API_KEY"}

response = requests.get(url, headers=headers)

print(response.text)

In this example, we pass in a “headers” dictionary with the “Authorization” header set to the API key.

Exercises

Here are some exercises with solutions to help you practice what you just learned:

Write a Python program that consumes a REST API and prints the response data to the console. The API endpoint is “https://api.example.com/endpoint” and the response is in JSON format.

import requests

url = "https://api.example.com/endpoint"

response = requests.get(url)

data = response.json()

print(data)

Write a Python program that consumes a REST API that requires an API key in the header. The API endpoint is “https://api.example.com/endpoint” and the response is in JSON format. The program should ask the user to enter the API key.

import requests

url = "https://api.example.com/endpoint"

api_key = input("Enter API key: ")

headers = {"Authorization": f"Bearer {api_key}"}

response = requests.get(url, headers=headers)

data = response.json()

print(data)

Write a Python program that consumes a REST API and prints the response data to the console. The API endpoint is “https://api.example.com/endpoint” and the response is in XML format.

import requests
import xml.etree.ElementTree as ET

url = "https://api.example.com/endpoint"

response = requests.get(url)

root = ET.fromstring(response.text)

print(root)

Write a Python program that consumes a REST API and prints the response data to the console. The API endpoint is “https://api.example.com/endpoint” and the response is in HTML format.

import requests
from bs4 import BeautifulSoup

url = "https://api.example.com/endpoint"

response = requests.get(url)

soup = BeautifulSoup(response.text, "html.parser")

print(soup)

Write a Python program that consumes a REST API and prints the response data to the console. The API endpoint is “https://api.example.com/endpoint” and the response is in CSV format.

import requests
import csv

url = "https://api.example.com/endpoint"

response = requests.get(url)

csv_reader = csv.reader(response.text.splitlines())

for row in csv_reader:
    print(row)