0

When i try to start the API with uvicorn, i get following error:

"""AssertionError: Param: nodes can only be a request body, using Body():"""

from fastapi import FastAPI
from fastapi.responses import JSONResponse
from typing import List, Dict

app = FastAPI(title="Neo4jAPI")

@app.post("/create_nodes/")
def post(nodes: List[Dict] | None = Header(default=None)):
    #Some Logic
    return JSONResponse(nodes)

How do i fix this error?

Jakube
  • 3,353
  • 3
  • 23
  • 40
Tim
  • 35
  • 4
  • 1
    You may find [this answer](https://stackoverflow.com/a/70636163/17865804), as well as [this](https://stackoverflow.com/a/73761724/17865804) and [this](https://stackoverflow.com/a/70640522/17865804) helpful – Chris May 23 '23 at 08:57

1 Answers1

1

By defining nodes: ... = Header(default=None), you tell FastAPI that the values of the nodes parameter should be extracted from the request headers.

FastAPI supports headers as single values: e.g. extracting the value of the User-Agent header with user_agent: str = Header(). It also supports extracting lists of values from headers, if headers are specified multiple times (see https://fastapi.tiangolo.com/lo/tutorial/header-params/).

However it doesn't support extracting lists of dictionaries from request headers. How would you even model that in a request header?

What you probably want to do, is extract the nodes from the request body (probably as JSON). The simple fix for it is just to remove the = Header(...) part. If there is just one parameter, FastAPI assumes that this represents the body.

@app.post("/create_nodes/")
def post(nodes: List[Dict] | None):
    return JSONResponse(nodes)
Jakube
  • 3,353
  • 3
  • 23
  • 40