GET operations

Introduction to FastAPI

Matt Eckerle

Software and Data Engineering Leader

GET operation review

HTTP protocol - several types of operations

  • GET is the most common

Example: https://www.google.com:80/search?q=fastapi

The key parts of a GET request are:

  • Host, e.g. www.google.com
  • Port, e.g. 80 (default)
  • Path, e.g. /search
  • Query String, e.g. ?q=fastapi
Introduction to FastAPI

FastAPI GET operation

The simplest FastAPI application:

from fastapi import FastAPI

# Instantiate app app = FastAPI()
# Handle get requests to root @app.get("/") def root(): return {"message": "Hello World"}
1 https://fastapi.tiangolo.com/tutorial/first-steps/
Introduction to FastAPI

Using the cURL web client

Key cURL options:

$ curl -h
Usage: curl [options...] <url>
 -v, --verbose               Make the operation more talkative
 -H, --header <header/@file> Pass custom header(s) to server
 -d, --data <data>           HTTP POST data

Example usage:

$ curl http://localhost:8000
{"message":"Hello World"}
Introduction to FastAPI

Query Parameters

New endpoint:

  • Path: "/hello"
  • Query parameter: "name"
    • Default value: "Alan"
@app.get("/hello")
def hello(name: str = "Alan"):
    return {"message": f"Hello {name}"}

Name not in request: Terminal showing a curl request without a name provided. Response message is "Hello Alan."

Name in request: Terminal showing a curl request with a name provided. Response message is "Hello Steve."

Introduction to FastAPI

Let's practice!

Introduction to FastAPI

Preparing Video For Download...