I once built a deploy button for PuppyFlow. Users selected inputs and outputs on the canvas, clicked the button, and the frontend sent the workflow to the backend in exchange for the API ID and key needed to call it. During debugging, the first step failed: POST /config_api returned 422. I added print(workflow_json, inputs, outputs) at the top of the backend function. Reproducing the issue later with the old code showed why it printed nothing. FastAPI had already returned before calling the function, however early I placed the print.
Why were all three fields reported missing when the request sent them?
Here is the request the deploy button sent. I substituted a smaller workflow and node IDs but kept the original fetch call.
const payload = {
workflow_json: { blocks: [{ id: "3" }, { id: "2" }], edges: [] },
inputs: ["3"],
outputs: ["2"],
};
const response = await fetch("/config_api", {
method: "POST",
body: JSON.stringify(payload),
});
The backend declared workflow_json: Dict, inputs: List[str], and outputs: List[str] separately. With this signature, FastAPI looks for three named fields at the top level of the request body. In the reproduction, the 422 response’s detail contained three errors. Their loc values (error locations) were ["body", "workflow_json"], ["body", "inputs"], and ["body", "outputs"]. All three names were spelled correctly. When they all go missing together, first check how the entire body was read.
Select that POST in the browser developer tools’ Network panel and compare two places. Payload shows serialized JSON text, but in Request Headers, Content-Type reads text/plain;charset=UTF-8. JSON.stringify() produces a string. When you give fetch a string without specifying its type, Fetch labels it as plain text.1
The project used FastAPI 0.115.8, which uses the request headers to decide how to read the body. For application/json, it first parses the body into a Python object. For this request’s text/plain, it passes the raw bytes into validation. The validator expects named fields on an object, so it finds none of the three.2
Would changing only the header fix the original endpoint?
Yes. During the retrospective I sent the same body to the versions before and after the fix using the project’s saved virtual environment. To isolate parsing, I replaced the database save function with a test double and inspected the arguments it received.
| Request Content-Type | Original endpoint: three declared fields | Fixed endpoint: read JSON directly |
|---|---|---|
text/plain;charset=UTF-8 | 422. All three fields missing, save function never called | 200. All three fields passed to the save function |
application/json | 200. All three fields passed to the save function | 200. All three fields passed to the save function |
With access to the frontend, the smallest test is to add headers: { "Content-Type": "application/json" } to the original fetch options. That also explains why the project’s Python test script could behave differently. It used requests.post(url, json=payload), so the client handled both JSON encoding and the header.
Which side did I actually change?
I started with the backend parameters. When I searched fast api 422 error on the afternoon of February 8, the Stack Overflow question I opened also involved a POST returning 422 after sending JSON. Its function signature was def main(user), so FastAPI looked for user in the URL query parameters. In my reproduction all three locations were under body. With the same status code, one look at loc tells you where to investigate. The answer also mentioned another option: “use Starlette's Request object directly”.3
I first tried declaring the whole payload as a dict, then defined a Pydantic model named ConfigData containing all three fields. The code I saved at 18:12 still took payload: ConfigData in the route. Right afterward I looked at the APIRouter.post example, twice jumping to the adjacent put example and quickly returning to post. Both showed how to receive a request body with a model. My deploy endpoint used POST throughout.4
The tiangolo in the documentation URL is the online handle of FastAPI’s creator, Sebastián Ramírez. The README for version 0.115.8 already listed fastapi.tiangolo.com as the official documentation site.5
As I recall, it was while reading the APIRouter examples that I finally understood how the parameters in a route function affect how FastAPI reads frontend data. I later changed the endpoint to accept Request directly and read its JSON myself. That resolved this 422.
By 18:24 I had changed body parsing to the following lines. The later checks and save logic are omitted here.
@router.post("/config_api")
async def config_api(request: Request, db: Session = Depends(get_db)):
payload = await request.json()
workflow_json = payload.get("workflow_json", {})
inputs = payload.get("inputs", {})
outputs = payload.get("outputs", {})
# 后续检查三个值是否为空,再保存工作流
I committed the change on February 10 as b80ad35, with the message fix request parsing logic of config api. This version explicitly parses the body as JSON in the function. Even if the frontend still sends text/plain, the three values can be read as long as the body is a JSON object like the one in the example. That also relaxed validation. FastAPI previously checked for a dictionary and lists of strings. The new code only checked for nonempty values. In retrospect those backend lines did make the existing button work. Adding the JSON header on the frontend would have been enough to keep the original field-type validation.
Footnotes
-
WHATWG. (n.d.). Fetch Standard. §5.2, “BodyInit unions,” extracting a string body. Default media type of a string body. ↩
-
FastAPI. (2025). routing.py (version 0.115.8, lines 245–266). GitHub. Code that chooses a parser by Content-Type. ↩
-
Smith. (2020, January 27). Python: FastAPI error 422 with POST request when sending JSON data [Question]. Stack Overflow. Question and original route. Chris. (2022, January 8). Answer to “Python: FastAPI error 422 with POST request when sending JSON data”. Stack Overflow. Opening paragraph and first paragraph of Option 4. ↩
-
FastAPI. (n.d.). APIRouter class. FastAPI Reference. post example and put example. ↩
-
Ramírez, S. (n.d.). Home. Author biography on tiangolo’s personal site. FastAPI. (2025). FastAPI README (version 0.115.8). GitHub. Documentation link. ↩