Skip to main content

Making your first GET request

Making your First GET Request with Python Requests

Welcome to the exciting world of Python Requests. This powerful library allows us to interact with the web in a simple and intuitive manner. Today, we're going to learn how to create a basic GET request.

What is a GET Request?

A GET request is an HTTP method used to retrieve data from a server. It's like asking a question to the server and waiting for the response. The data you're asking for is identified by the URL you're requesting. For example, when you type a website address into your browser, you're sending a GET request to that website's server, asking it to send you the HTML of the web page.

Installing Requests

First things first, we need to install the requests library. If you haven't installed it yet, you can do so using pip, Python's package installer. Open your terminal or command prompt and type the following command:

pip install requests

Making the GET Request

Now that we have requests installed, let's make our first GET request. We will request data from a placeholder API - https://jsonplaceholder.typicode.com/posts. This API returns a list of posts in JSON format.

import requests

# Specify the URL of the data we want to get
url = "https://jsonplaceholder.typicode.com/posts"

# Send a GET request to the URL
response = requests.get(url)

# Print the status code
print(f'Status code: {response.status_code}')

Here, requests.get(url) sends a GET request to the specified URL and returns a Response object, which we're storing in the response variable. The status code tells us whether our request was successful. A status code of 200 means the request was successful.

Understanding the Response

The response object returned by requests.get(url) contains the server's response to our request. We can extract the data we want from this response.

# Get the data from the response
data = response.json()

# Print the first post
print(data[0])

The json method converts the JSON data in the response to a Python list or dictionary. Here, data[0] gives us the first post in the list of posts.

Error Handling

It's a good practice to handle potential errors when making GET requests. For example, the server might not respond, or the URL might be incorrect. Let's add some error handling to our code:

import requests

url = "https://jsonplaceholder.typicode.com/posts"

try:
response = requests.get(url)
response.raise_for_status() # Raise exception if the request was unsuccessful
except requests.exceptions.HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except requests.exceptions.RequestException as err:
print(f'Error occurred: {err}')
else:
data = response.json()
print(data[0])

Here, raise_for_status raises an HTTPError if one occurred. We use a try-except block to catch this error and any other RequestException, and print an error message if one occurred.

Congratulations! You've made your first GET request using Python's requests library. Play around with different URLs and see what data you can retrieve. Happy coding!