简体   繁体   中英

FastAPI post request body, jsonlines of any number of jsons

Using FastAPI requires defining the Request Body. like this

class Item(BaseModel):
    catalog_item_number: str
    label: str

This accepts only one json per request. I want to make it accept any number of JSONs in the same body request

{"catalog_item_number":"xxxxxx", "label": "xxxxx"}
{"catalog_item_number":"xxxxxx", "label": "xxxxx"}
{"catalog_item_number":"xxxxxx", "label": "xxxxx"}
{"catalog_item_number":"xxxxxx", "label": "xxxxx"}
...etc

There should only be one json object per request.
To solve this, put the objects into a json array like this:

[
  {"catalog_item_number":"xxxxxx", "label": "xxxxx"},
  {"catalog_item_number":"xxxxxx", "label": "xxxxx"},
  {"catalog_item_number":"xxxxxx", "label": "xxxxx"},
  {"catalog_item_number":"xxxxxx", "label": "xxxxx"}
]

Then your request could accept this as an Item[] type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM