I am trying to pass some json contents from the client to the server via some
simple REST API built with FastAPI (using uvicorn).
If I wrap the file contents into a pydantic.BaseModel like so
app = FastAPI()
class ConfigContents(BaseModel):
str_contents: str
@app.post("/writeFile/{fn}")
async def write_file(fn: str, contents: ConfigContents):
filepath = "/some_server_dir/" + fn
with open(filepath, "w") as f:
f.write(contents.str_contents)
return contents
I essentially get what I want, i.e. on the client side (using the request-library), I can execute
response = requests.post("http://127.0.0.1:8000/writeFile/my_file.json",
data=json.dumps({"str_contents": contents}))
and end up with the file contents assigned to response and written to the file on the "server".
My question is: is there a simpler way to achieve the same, e.g. just passing
the json contents as a string to the server without the need to wrap it into a model?