1

So the request body has a defined structure that inherits from PyDantic's BaseModel class. The data is inputted in JSON format. I've actually tested my code using fastAPI's user interface on the '.../docs' page--and it works fine. But how does this data get inputted by a user?

For example, let's say I have code like this

class SqlQuery(BaseModel):
    column_to_select: str

@app.post("/query")
async def my_query(sql_query: SqlQuery):
    # code to connect to the database goes here

    execute_my_query_function(f"SELECT {sql_query.column_to_select} FROM mydatabase.mytable;")

Can I set this up where the end-user selects a column from a drop down, and that gets sent as JSON to my API? Because obviously we don't build apps where the end-user has to manually enter JSON. I'm trying to conceptualize how this would work in a real built-out application.

  • You'd usually use a frontend framework, such as Vue, React, Svelte, or manually use plain Javascript together with HTML if you go the JSON route. The other option is to return HTML rendered from templates directly to the user, and then accept form data (with `Form()` for each value) back from the user (regular HTML forms). Both will work just fine. Regardless, the interface will be presented in a regular browser with regular HTML elements, but how you get there will depend on the choice of technology and optionally framework. – MatsLindh Nov 23 '22 at 21:17
  • Related posts can be found [here](https://stackoverflow.com/a/74515250/17865804), [here](https://stackoverflow.com/a/70640522/17865804), as well as [here](https://stackoverflow.com/a/70636163/17865804) and [here](https://stackoverflow.com/a/73761724/17865804). – Chris Nov 26 '22 at 11:33

0 Answers0