top of page
Writer's pictureAshish Disawal

Part-02: FastAPI Fundamentals - Getting User Input

Updated: Sep 18



FastAPI Fundamentals

The FastAPI Fundamentals Series



FastAPI Fundamentals - Getting User Input


FastAPI offers various methods to get user input in your API. Let's explore four commonly used methods: Query parameters, Path parameters, Body parameters, and HTTP Headers. Lets get started with FastAPI Fundamentals - Getting User Input.



1. Query Parameters


Query parameters are easy to use and commonly employed for optional parameters. Update your main.py to include a route with query parameters:


from fastapi import FastAPI

app = FastAPI()

@app.get("/query_params/")
def read_query_params(param1: str, param2: int = None):
    return {"param1": param1, "param2": param2}

Visit http://127.0.0.1:8000/query_params/?param1=hello&param2=42 to see the results, or you can use CURL


curl -X GET "http://127.0.0.1:8000/query_params/?param1=hello&param2=42"

2. Path Parameters


Path parameters are part of the URL and are often used for required parameters. Update your main.py to include a route with path parameters:


from fastapi import FastAPI

app = FastAPI()

@app.get("/path_params/{param1}/{param2}")
def read_path_params(param1: str, param2: int):
    return {"param1": param1, "param2": param2}


curl -X GET "http://127.0.0.1:8000/path_params/hello/42"

3. Body Parameters


Body parameters are used to send data in the request body, typically for more complex structures. Update your main.py to include a route with a body parameter:


from fastapi import FastAPI, Body


from fastapi import FastAPI, Body

app = FastAPI()

@app.post("/body_params/")
def read_body_params(param1: str = Body(embed=True), param2: int = Body(embed=True)):
    return {"param1": param1, "param2": param2}

You can send a POST request with a JSON body to http://127.0.0.1:8000/body_params/ to see the results.


curl -X POST "http://127.0.0.1:8000/body_params/" \
    -H "Content-Type: application/json" \
    -d '{"param1": "hello", "param2": 42}'

4. HTTP Header


Headers are often used for authentication or passing metadata. Update your main.py to include a route with an HTTP header:


from fastapi import FastAPI, Header

app = FastAPI()

@app.get("/header/")
def read_header(api_key: str = Header()):
    return {"api_key": api_key}

Include the header api_key in your request to http://127.0.0.1:8000/header/ to see the results.


curl -X GET "http://127.0.0.1:8000/header/" -H "api-key: your_api_key_here"

Choosing the Right Method

  • Query Parameters: Use for optional parameters or filtering data. Great for simple requests where parameters are appended to the URL.

  • Path Parameters: Ideal for required parameters that are part of the URL structure. Commonly used for resource identification.

  • Body Parameters: Perfect for complex data structures or larger payloads. Ideal for POST requests when data needs to be sent in the request body.

  • HTTP Headers: Useful for passing additional information, such as authentication tokens or metadata.

Choose the method that best suits your API's requirements. Often, a combination of these methods is used in more complex applications.


Conclusion


Congratulations! You've now mastered different methods of getting user input in FastAPI. Understanding when and why to use each method is key to designing effective and efficient APIs. Feel free to experiment with these methods and explore more advanced features as you continue your FastAPI journey.


Happy coding!

165 views

Recent Posts

See All

Comments


bottom of page