2

im using FastApi and get some troubles with url. i have a root url

 @app.get("/myurl")

http://host/myurl and http://host/myurl?id=2 and here function returns all info from needed table.

on url like http://host/myurl?id=2&type=3 i need to get another query from table. how i need to create function because now this http://host/myurl?id=2 overlapping this function http://host/myurl?id=2&type=3

how i can use multiple urls with different values in it in fastapi?

and i want to know how to make url like http://host/myurl?id=2&type=3,2 to return result from table for two types (query example is SELECT * from mytable WHERE id=%(id)s and type IN (1,2) but type IN (,) should be parameters which i need to inpout

flamixx
  • 57
  • 5

1 Answers1

1

how i can use multiple urls with different values in it in fastapi?

As far as I know, you can't. But fortunately, you don't need to. What you can do is define only one route ("/myurl") with both parameters id and type, and set the second as optional. Then, if you don't receive type, you process a different query.

By the way, don't use id and type as parameter names, that will mess with the name of the in-built function id()and type().

Here a working example:

from fastapi import FastAPI, Query


app = FastAPI()


@app.get("/myurl")
async def my_url(my_id: int = Query(...), my_type: int = Query(None)):

    if my_type:
        return f"You gave an id ({my_id}) and a type ({my_type})."

    return f"You gave only an id ({my_id}) but no type."

i want to know how to make url like http://host/myurl?id=2&type=3,2

Not sure you can do it at all. What you can do is add the type parameter several times, like this:

http://host/myurl?my_id=2&my_type=3&my_type=2

In this case, you need to slightly change your code:

from fastapi import FastAPI, Query
from typing import List


app = FastAPI()


@app.get("/myurl")
async def my_url(my_id: int = Query(...), my_type: List[int] = Query(None)):

    if my_type:
        if len(my_type) > 1:
            return f"You gave an id ({my_id}) and a list of types ({my_type})."
        else:
            return f"You gave an id ({my_id}) and a type ({my_type})."

    return f"You gave only an id ({my_id}) but no type."

Note that you'll always receive my_type as a list then, even if you pass it only one time.

ye olde noobe
  • 1,186
  • 6
  • 12
  • Thanks! Could you explain how my_id: int = query should looks like? – flamixx May 13 '22 at 20:04
  • 1
    `my_id` is the name of the field, `int` is the type and `Query(...)` says that it comes from the query string (the part after `?` in the URL). `...` should be included verbatim. – MatsLindh May 14 '22 at 06:48
  • i got this error `AttributeError: 'MySQLConverter' object has no attribute '_list_to_mysql'` it seems that i need to install a module? – flamixx May 15 '22 at 19:36
  • @flamixx this AttributeError is a different issue, completely disconnected from your FastAPI question. Please open a new question where you can properly describe the problem, what your code looks like, how it can be reproduced, etc. – ye olde noobe May 16 '22 at 08:44
  • the problem is i use modul mysql connector and if using `my_type: List[int] = Query(None))` got an error mysql.connector.errors.ProgrammingError: Failed processing pyformat-parameters; Python 'list' cannot be converted to a MySQL type – flamixx May 16 '22 at 09:53
  • This is an error tied to MySQL. We cannot help you without knowing what is the context (for example, what library you are using), what you are trying to achieve, and see at least a little bit of code, which is one of the reasons such a problem cannot be treated in a comment on a question about FastAPI. I'm going to guess you're using pymysql and point you to [this answer](https://stackoverflow.com/questions/39334484/pymysql-select-in-with-variable-number-of-parameters#answer-41434772), but if you need further help on that, please do post another question. – ye olde noobe May 16 '22 at 14:49