-
|
I am experiencing an issue trying to represent an ISO8601 datetime as a query parameter. Given the following fastAPI endpoint..... @router.get("/query/{city}")
async def query_by_city_and_date(
city: str,
at: Optional[Union[datetime.date, datetime.datetime]] = None
):
return {"city": city, "at": at}
~ I can successfully browse the api with a simple date string, using:
However, when I try and make a request with an ISO datetime string:
I get the following error: {"detail":[{"loc":["query","at"],"msg":"invalid date format","type":"value_error.date"},{"loc":["query","at"],"msg":"invalid datetime format","type":"value_error.datetime"}]}However, it does accept: If I update the endpoint to accept the date parameter as a string it accepts the value but then I would have to do the parsing manually. @router.get("/query/{city}")
async def query_by_city_and_date(
city: str,
at: Optional[str] = None
):
return {"city": city, "at": at}
~ Currently, to accept a datetime ISO 8601 formatted string does it have to be URL encoded to be successfully parsed? If so I will have to resort to changing the date parameter to a string and manually parsing.... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
You shouldn't be getting any issues in your swagger docs as it will auto URL encode your parameters. You should always URL encode the parameters, what is the reason you aren't? Note this is the CuRL command that the swagger doc produces: curl -X 'GET' \
'http://127.0.0.1:8000/query/v?at=2008-09-15T15%3A53%3A00%2B05%3A00' \
-H 'accept: application/json' |
Beta Was this translation helpful? Give feedback.
You shouldn't be getting any issues in your swagger docs as it will auto URL encode your parameters. You should always URL encode the parameters, what is the reason you aren't?
Note this is the CuRL command that the swagger doc produces: