Introduction

The Requests module offers a simple and user-friendly way of making HTTP requests in Python. Whether you are retrieving data from a web page, interacting with an API, or sending information to a server, the Requests module makes the task straightforward. In this tutorial, we will explore how to check the HTTP status code of an API Endpoint using Python Requests module. In specific, we will have a look at multiple examples.

API Status Codes

In general, status codes are grouped into five main categories.

  • 1xx: Informational
  • 2xx: Success
  • 3xx: Redirection
  • 4xx: Client Error
  • 5xx: Server Error

Sample APIs

In the following, we will use examples from the page Sample APIs.

Sample APIs
A playground for RESTful and GraphQL endpoints.

Import Libraries

First, import the python Request module.

import requests

Define Endpoint

Let's define two endpoints:

endpoint_coffee = 'https://api.sampleapis.com/coffee'

endpoint_tea = 'https://api.sampleapis.com/tea'

Send GET Request

Now, you can use the requests.get() method to send a GET request to the defined endpoints. This method returns a response object that contains information about the request, including the HTTP status code.

response_coffee = requests.get(endpoint_coffee)

response_tea = requests.get(endpoint_tea)

Check HTTP status code

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In