Не получается пост запрос к fastAPI
Не получается запрос пост к fast Api Сервер:
import httpx
import requests
from pydantic import BaseModel
from fastapi import FastAPI
from typing import Union
from fastapi.middleware.cors import CORSMiddleware
class ChipsAndRules(BaseModel):
chips: str
rules: Union[str, None] = None
lang: Union[str, None] = None
@app.post("/newChips/")
async def create_new_chips(old_chips: ChipsAndRules):
return {"chips and rules " : old_chips.chips+' '+old_chips.rules}
Вот вызов:
req = new XMLHttpRequest();
req.onreadystatechange = function (e) {
if (req.readyState == 4) {
console.log( 'Проверка, ответа' + req.responseText );
}
};
reqBody = {"chips" : "chip1",
"rules": "rule1",
};
req.open("POST", "http://ххх:9091/newChips/",true);
req.send(reqBody);
Получаю ответ:
422 (Unprocessable Entity)
{"detail":[{"type":"model_attributes_type","loc":["body"],"msg":"Input should be a valid dictionary or object to extract fields from","input":"[object Object]"
Не пойму, что не так.
upd. не хватало
req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
req.send(JSON.stringify(reqBody));