Skip to content

Bad encoding in query parameters with new TestClient using httpx.Client #5646

@azogue

Description

@azogue

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 😅

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions