Swagger and HTTPie Testing Clients
00:00 As mentioned, REST APIs aren’t meant to be accessed by a web browser. They’re designed to be consumed by other applications. Therefore, it helps to have a client to interact with them during development, and web browsers just aren’t up to the task. You could use a tool such as Postman, and if you are already using Postman, there is no reason you can’t continue to use it with FastAPI.
00:24
But FastAPI also includes a special endpoint that lets you interact with APIs. Go to http://localhost:8000/docs
. You’ll see the Swagger tool.
00:40 This is a browser-based tool for interacting with and testing REST APIs. FastAPI uses it to provide interactive documentation. Notice that the endpoint you created for the Root of the application is listed, and it uses the GET method. Later in the course, you’ll see how to use the POST method. Expand the item to see an abundance of information about how to access the endpoint, what data it expects, and what data it returns, and any errors that could occur.
01:10 It also gives you a way to invoke the endpoint in the browser. Click the Try it out button. The interface now changes and shows an Execute button.
01:21 If the endpoint expected any inputs, Swagger would prompt you to provide them, as you’ll see later. But this simple endpoint doesn’t accept any, so click the Execute button to invoke the endpoint.
01:33
Scroll down and you can see the JSON that was returned for the Hello world
message by FastAPI. In addition, it returned a 200
OK HTTP status code, and you can see the headers of the response as well. Later in the course, you’ll see how Swagger handles errors for parameters and incorrect types.
01:54 I want to show you one more way to interact with FastAPI from the command line. It’s a tool called HTTPie, and you will need to leave the FastAPI server running in the background.
02:06 So open a new terminal in Visual Studio Code by clicking the + button,
02:12
then you can install httpie
from the HTTPie package using pip
.
02:24
The http
command will now be available at the command line. Invoke the API using the http
command.
02:35
You can see the JSON that was returned for the Hello world
message. And again, the API also returned to 200
status code, which is OK, and some headers, since the API currently only responds to GET requests.
02:50
However, what happens if you try to make a POST request? Try it by adding the HTTP POST
method to the http
command.
03:03
As you’ll see later in the course, in addition to the @get
decorator, there’s also a @post
decorator, but you haven’t used it yet.
03:09
So the API returns a 405
status code, which means that the method POST
is not allowed. Later in the course, you’ll see that, in addition to the @get
decorator, there are @post
, @put
, and @delete
decorators.
03:23 You’ll continue to use Swagger and HTTPie in the rest of the course. In the next lesson, you’ll see how to provide input to your REST API endpoints with path parameters.
Become a Member to join the conversation.