Issue 1:
First Check

I added a very descriptive title to this issue.

I used the GitHub search to find a similar issue and didn't find it.

I searched the FastAPI documentation, with the integrated search.

I already searched in Google "How to X in FastAPI" and didn't find any information.

I already read and followed all the tutorial in the docs and didn't find an answer.

I already checked if it is not related to FastAPI but to Pydantic.

I already checked if it is not related to FastAPI but to Swagger UI.

I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI

Description

We have a test that does something like this:

now = utc_datetime()
url = client.app.url_path_for("balances")
start_date = now - timedelta(minutes=1)
start_date_param = urlencode({"start_date": start_date.isoformat()})
end_date_param = urlencode({"end_date": now.isoformat()})
response = client.get(url + f"?{start_date_param}&{end_date_param}")

With FastAPI 0.86 everything works. Starting from 0.87 (which uses starlette 0.21.0) our endpoint errors with:

{'error': {'code': 'validation_error', 'message': 'Validation Error', 'errors': [{'attribute': 'query -> start_date', 'code': 'value_error.datetime', 'message': 'invalid datetime format', 'ctx': None}, {'attribute': 'query -> end_date', 'code': 'value_error.datetime', 'message': 'invalid datetime format', 'ctx': None}]}}

If I force the installation of starlette 0.22.0 and rerun the test, everything works. This is a known bug of starlette 0.21.0 and it has been fixed in 0.22.0:

https://github.com/encode/starlette/pull/1953/files

https://github.com/encode/starlette/releases/tag/0.22.0

Solution

Bump starlette version in FastAPI to 0.22.0

Note

I can make this small PR with the update as soon as you confirm/acknowledge this issue.

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.87.0

Python Version

3.10.0

Additional Context

No response

Issue 2:
First Check

I added a very descriptive title to this issue.

I used the GitHub search to find a similar issue and didn't find it.

I searched the FastAPI documentation, with the integrated search.

I already searched in Google "How to X in FastAPI" and didn't find any information.

I already read and followed all the tutorial in the docs and didn't find an answer.

I already checked if it is not related to FastAPI but to Pydantic.

I already checked if it is not related to FastAPI but to Swagger UI.

I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

I commit to help with one of those options 👆

Example Code

import logging

from fastapi import FastAPI

app = FastAPI()

@app.get("/example")
async def _show_encoding_error(look_for: str):
return {"found": look_for}

if __name__ == '__main__':
from fastapi.testclient import TestClient

with TestClient(app) as client:
params = {"look_for": "plain text"}
resp = client.get("/example", params=params).json()
logging.warning(resp)
assert resp["found"] == "plain text"

params = {"look_for": "España"}
resp = client.get("/example", params=params).json()
logging.warning(resp)
assert resp["found"] == "España", resp["found"]

Description

After the change to httpx for the TestClient in v0.87.0, the query parameters are not properly encoded? when sending requests with it, and strings are corrupted when received in the endpoints.

The example app works as expected if called from the SwaggerUI or from another python process using a plain httpx.Client, so it appears something broke with the new wrapping for TestClient 🥲

import httpx

params = {"look_for": "España"}
with httpx.Client(base_url="http://localhost:8000/") as client:
resp = client.get("/example", params=params).json()
assert resp["found"] == "España"

Operating System

macOS

Operating System Details

M1, running arm64 arch

FastAPI Version

0.87.0

Python Version

Python 3.10.5

Additional Context

starlette-0.21

httpx-0.23.0

Discovered when trying to migrate the test suite for a ~big project previously using fastapi-0.85.1 + starlette-0.20.4.

All minor syntax changes from old requests to new httpx were under control, but in one unit test, a string with an accent was making some search to fail without results (test is sending "Formalización" but endpoint is receiving "FormalizaciÃ³n" 😱), and I was getting crazy 😅