-
Notifications
You must be signed in to change notification settings - Fork 0
(G). How to interact with your REST API
Welcome back!
In the previous section, we created our first REST API endpoint. However, we must test it before allowing clients to interact with it to ensure that it functions as intended. This section will teach you how to test your REST API by sending HTTP requests and receiving responses. Here's the code from our last session:
from flask import Flask
app = Flask(__name__)
stores = [
{
"name": "Kurro Stores",
"items": [
{
"name": "iMac",
"price": 37.99
}
]
}
]
# Our first endpoint
@app.get("/store") # http://127.0.0.1:5000/store
def get_stores():
return {"stores": stores}
- You can use manual exploratory testing.
- You can use automated tests.
Exploratory testing allows us to interact with the API and validate its behavior. The first step is to conduct exploratory testing and then automate those manual tests. This tutorial solely focuses on exploratory testing; we will use Insomnia, a user-friendly HTTP client, for sending and receiving requests.
Before testing our REST API using Insomnia, we must install it on our machine. You can get Insomnia from its website (insomnia.rest) and install it based on your device's operating system.
-
Open the Insomnia client.
-
Navigate to the home button, then personal projects, and to create a new project, click the plus (
+
) sign next to the Filter tab.

- You can call this project 'REST API Tutorial.'

- Navigate to the far-right corner, select the Create tab, and in a dropdown list, select 'Request Collection.'

- You can call this collection 'Stores REST API.'

We will make a 'GET
' request to the API to retrieve the stores using our first defined endpoint. Here's the code for our first endpoint:
# Our first endpoint - Get all store data
@app.get("/store") # http://127.0.0.1:5000/store
def get_stores():
return {"stores": stores}
Before we make the HTTP requests in the Request Collection, let us first define what makes up a typical HTTP request:
- Request Line: The first line of an HTTP request, includes the HTTP method, target URL, endpoint, and a version, as shown in the following URL.
GET http://127.0.0.1:5000/store
-
Request Headers: Offer additional information about the request context that the server can use to tailor responses; some standard headers include
Host
,Content-Type
, andAuthorization
.
Host: www.api.kurro.com
Content-Type: application/json
Authorization: Bearer <token>
-
Request Body (Optional): The request body contains additional data, usually sent along with the request. You can use the request body when making requests with methods like
POST
orPUT
to send data to the server. The presence or absence of a request body depends on the specific server and the HTTP method used.
- To make a request, click on the project you created and then click the Request Collection.

- Select the plus (
+
) sign next to the Filter tab, and in a dropdown list, select the 'HTTP Request
' tab. Alternatively, enter 'CTRL+N
' from the keyboard.

- To enter the title of your request, double-click the 'New Request' tab. Keep the method as
GET
.

- You can call this request '
/store Get all store data
.'

- Enter the Base URL (
http://127.0.0.1:5000
) and include the endpoint (/store
) we will use for our request.

- After finishing, confirm that your Flask application is running. Remember to activate your virtual environment before running the app.

-
The Flask app will run, by default, on port
5000
. An error will occur if you have another app running because the port will be busy. Check for any other Flask apps running on the same port when encountering an error. -
Once your Flask app is up and running, send a request using the Insomnia client, and you can see the JSON response from your API.

If you faced no issues and the response was in JSON format, you have completed your first REST API. Now, we can continue developing our REST API. Always remember to create new HTTP Requests in Insomnia and test our code as we progress!
To move on to the next section of the tutorial, use this link: How to Create Stores within a REST API.