Using the FastAPI documentation, I'm attempting to send a POST
request to an endpoint that takes a JSON object with a list
as input:
{
"urls":[
"https://www.website.com/",
"https://www.anotherwebsite.com"
]
}
Simplified reproducible example app code:
from fastapi import FastAPI
app = FastAPI()
@app.post("/checkUrls")
def checkUrls(urls: list[str]):
return urls
#alternatively (given that I expect each url to be unique)
@app.post("/checkUrlsSet")
def checkUrlsSet(urls: set[str]):
return urls
When I test the endpoint using the Python requests library like this:
import requests
rListTest = requests.post("http://127.0.0.1:8000/checkUrls", json = {"urls" : ["https://www.website.com/", "https://www.anotherwebsite.com"]})
print(rListTest.status_code)
print(rListTest.json())
rSetTest = requests.post("http://127.0.0.1:8000/checkUrlsSet", json = {"urls" : ["https://www.website.com/", "https://www.anotherwebsite.com"]})
print(rSetTest.status_code)
print(rSetTest.json())
in both cases, I get the a 400 status code error and the response is:
{'detail': 'There was an error parsing the body'}
I'm using python 3.10 on a linux ubuntu 22.04 vm with an ampere ARM processor if it matters...
I've also tried the following app code:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class UrlsList(BaseModel):
urls: list[str]
class UrlsSet(BaseModel):
urls: set[str]
@app.post("/checkUrls")
async def checkUrls(urls: UrlsList):
return urls
@app.post("/checkUrlsSet")
async def checkUrlsSet(urls: UrlsSet):
return urls
which returns a 422 Unprocessable Entity
error:
{'detail': [{'loc': ['body'], 'msg': 'value is not a valid list', 'type': 'type_error.list'}]}
{'detail': [{'loc': ['body'], 'msg': 'value is not a valid set', 'type': 'type_error.set'}]}