-
|
Description I was going through the Path Parameter and Query Parameters. So far I got, We have to create a parameter for both My question is what if someone wants to create an API like this.
To create such API I have to pass two parameters with same name but which doesn't allows in Python. @app.get('/message/{user_id}')
async def create_message(user_id: str, user_id: str, message: str):
......
......
return {}Is there any way to do this or I am missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
|
Yep. You can use an alias, and name it the same: https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#alias-parameters |
Beta Was this translation helpful? Give feedback.
-
|
For example: from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/test/{user_id}")
def test(user_id: str, query_user_id: str = Query(..., alias="user_id")):
return {"user_id": user_id, "query_user_id": query_user_id}It works, validates the path and query parameters, generates the OpenAPI schema, etc. Swagger UI has a bit of trouble showing it correctly, but if you go to the URL directly: http://127.0.0.1:8000/test/foo?user_id=bar You get the response: {"user_id":"foo","query_user_id":"bar"} |
Beta Was this translation helpful? Give feedback.
-
|
This is awesome. |
Beta Was this translation helpful? Give feedback.
-
|
You're welcome! And thanks for reporting back and closing the issue. |
Beta Was this translation helpful? Give feedback.
-
|
Is it possible to use alias with path parameters? like this: |
Beta Was this translation helpful? Give feedback.
-
|
Have you tried it? If so, what was the outcome? If not, please do:) |
Beta Was this translation helpful? Give feedback.
-
|
@JarroVGIT Example usage of path parameters with alias |
Beta Was this translation helpful? Give feedback.
For example:
It works, validates the path and query parameters, generates the OpenAPI schema, etc.
Swagger UI has a bit of trouble showing it correctly, but if you go to the URL directly: http://127.0.0.1:8000/test/foo?user_id=bar
You get the response:
{"user_id":"foo","query_user_id":"bar"}