Created
November 3, 2020 06:49
-
-
Save Mause/39da6ae8fc48b8e6b6dd502015f6b1ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi.testclient import TestClient | |
from fastapi import FastAPI, Depends, Form | |
from pydantic import BaseModel | |
app = FastAPI() | |
def form_body(cls): | |
cls.__signature__ = cls.__signature__.replace( | |
parameters=[ | |
arg.replace(default=Form(...)) | |
for arg in cls.__signature__.parameters.values() | |
] | |
) | |
return cls | |
@form_body | |
class Item(BaseModel): | |
name: str | |
another: str | |
@app.post('/test', response_model=Item) | |
def endpoint(item: Item = Depends()): | |
return item | |
tc = TestClient(app) | |
r = tc.post('/test', data={'name': 'name', 'another': 'another'}) | |
assert r.status_code == 200, r | |
print(r.json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment