Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building Data Science Applications with FastAPI

You're reading from   Building Data Science Applications with FastAPI Develop, manage, and deploy efficient machine learning applications with Python

Arrow left icon
Product type Paperback
Published in Oct 2021
Publisher Packt
ISBN-13 9781801079211
Length 426 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
François Voron François Voron
Author Profile Icon François Voron
François Voron
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Section 1: Introduction to Python and FastAPI
2. Chapter 1: Python Development Environment Setup FREE CHAPTER 3. Chapter 2: Python Programming Specificities 4. Chapter 3: Developing a RESTful API with FastAPI 5. Chapter 4: Managing Pydantic Data Models in FastAPI 6. Chapter 5: Dependency Injections in FastAPI 7. Section 2: Build and Deploy a Complete Web Backend with FastAPI
8. Chapter 6: Databases and Asynchronous ORMs 9. Chapter 7: Managing Authentication and Security in FastAPI 10. Chapter 8: Defining WebSockets for Two-Way Interactive Communication in FastAPI 11. Chapter 9: Testing an API Asynchronously with pytest and HTTPX 12. Chapter 10: Deploying a FastAPI Project 13. Section 3: Build a Data Science API with Python and FastAPI
14. Chapter 11: Introduction to NumPy and pandas 15. Chapter 12: Training Machine Learning Models with scikit-learn 16. Chapter 13: Creating an Efficient Prediction API Endpoint with FastAPI 17. Chapter 14: Implement a Real-Time Face Detection System Using WebSockets with FastAPI and OpenCV 18. Other Books You May Enjoy

Installing the HTTPie command-line utility

Before getting into the heart of the topic, there is one last tool that we'll install. FastAPI is, as you probably know, mainly about building REST APIs. To do so, you have several options:

  • FastAPI automatic documentation (we'll talk about this later in the book)
  • Postman, a GUI tool to perform HTTP requests
  • cURL, the well-known and widely used command-line tool to perform network requests

Even if visual tools are nice and easy to use, they sometimes lack some flexibility and may not be as productive as command-line tools. On the other hand, cURL is a very powerful tool with thousands of options but can be complex and verbose for testing simple REST APIs.

This is why we'll introduce HTTPie, a command-line tool aimed at making HTTP requests with an intuitive syntax, JSON support, and syntax highlighting. It's available to install from most package managers:

  • macOS users, use this:
    $ brew install httpie
  • Ubuntu users, use this:
    $ sudo apt-get update; sudo apt-get install httpie

Let's see how to perform simple requests on a dummy API:

  1. First, let's retrieve data:
    $ http GET https://603cca51f4333a0017b68509.mockapi.io/todos
    HTTP/1.1 200 OK
    Content-Length: 195
    Content-Type: application/json
    [
        {
            "id": "1",
            "text": "Island"
        }
    ]

As you can see, you can invoke HTTPie with the http command and simply type the HTTP method and the URL. It outputs both the HTTP headers and the JSON body in a clean and formatted way.

  1. HTTPie also supports sending JSON data in a request body very quickly without having to format the JSON yourself:
    $ http -v POST https://603cca51f4333a0017b68509.mockapi.io/todos text="My new task"
    POST /todos HTTP/1.1
    Accept: application/json, */*;q=0.5
    User-Agent: HTTPie/2.3.0
    {
        "text": "My new task"
    }
    HTTP/1.1 201 Created
    Content-Length: 31
    Content-Type: application/json
    {
        "id": "6",
        "text": "My new task"
    }

By simply typing the property name and its value separated by =, HTTPie will understand that it's part of the request body in JSON. Notice here that we specified the -v option, which tells HTTPie to output the request before the response, which is very useful to check that we properly specified the request.

  1. Finally, let's see how we can specify request headers:
    $ http -v GET https://603cca51f4333a0017b68509.mockapi.io/todos "My-Header: My-Header-Value"
    GET /todos HTTP/1.1
    Accept: */*
    My-Header: My-Header-Value
    User-Agent: HTTPie/2.3.0
    HTTP/1.1 200 OK
    Content-Length: 227
    Content-Type: application/json
    [
        {
            "id": "1",
            "text": "Island"
        }
    ]

That's it! Just type your header name and value separated by a colon to tell HTTPie it's a header.

You have been reading a chapter from
Building Data Science Applications with FastAPI
Published in: Oct 2021
Publisher: Packt
ISBN-13: 9781801079211
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image