Making Requests to a REST API

Susan Adesoji
3 min readApr 2, 2022

--

Sending a request to a REST API is a common task in web development and can be accomplished using a variety of programming languages and libraries. In this article, we will discuss the basics of making a request to a REST API, including the different types of requests, request headers, and request bodies. We will also provide examples of how to send requests using the Python programming language and the popular requests library.

First, let’s understand the basics of REST API. REST, or Representational State Transfer, is a set of architectural principles for building web services. A REST API is an application programming interface that adheres to these principles and allows for communication between different software systems. REST APIs typically use the HTTP protocol and can be accessed using standard HTTP methods such as GET, POST, PUT, and DELETE.

To send a request to a REST API, you will typically use one of these HTTP methods and include a URL that specifies the endpoint of the API you are trying to access. For example, to retrieve a resource from an API, you would use the GET method and include the URL of the resource in the request. To create a new resource, you would use the POST method and include the URL of the resource’s parent or collection. When sending a request to a REST API, you may also need to include additional information in the request headers. Request headers are used to provide additional information about the request, such as the type of content being sent in the request body or the preferred language for the response. Common headers include Content-Type, which specifies the format of the request body, and Accept, which specifies the format of the response.

In addition to headers, a request may also include a request body. The request body is the data that is sent to the API in the request. For example, when creating a new resource using the POST method, the request body would include the data for the new resource. When updating a resource using the PUT method, the request body would include the updated data for the resource.

Now that we have an understanding of the basics of making a request to a REST API, let’s take a look at how to send requests using Python and the requests library.requests is a popular library for sending HTTP requests in Python, and it provides a simple and intuitive interface for making requests.

To send a GET request to an API, you can use the get() method provided by the requests library. Here's an example of how to use the get() method to retrieve a resource from an API:


import requests

response = requests.get('https://example.com/api/endpoint')

print(response.status_code)
print(response.json())

In this example, we imported the requests library and used the get() method to send a GET request to the specified API endpoint. The response object is stored in the variable response and we print the status code and JSON data of the response.

To send a POST request and include a JSON payload in the request body, you can use the post() method provided by the requests library. Here's an example of how to use the post() method to create a new resource in an API:

import requests

data = {"name": "John", "age": 30}
headers = {'Content-type': 'application/json'}
response = requests.post('https://example.com/api/endpoint', json=data, headers=headers)

print(response

--

--