Skip to content

Instantly share code, notes, and snippets.

@Mause
Created November 3, 2020 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mause/39da6ae8fc48b8e6b6dd502015f6b1ba to your computer and use it in GitHub Desktop.
Save Mause/39da6ae8fc48b8e6b6dd502015f6b1ba to your computer and use it in GitHub Desktop.
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