我是FastAPI的新手,遇到422 Unprocessable Entity
错误消息时遇到问题。我想使用PHP客户端发布数据,但所有字段都收到msg”:“field required”,“type”:“value_error.missing”。我使用的是PHP客户端。我可以使用www.example.com POST方法成功发布数据http://127.0.0.1:8000/docs#/。感谢您的帮助。
{"detail":[{"loc":["body","cin_numcedeao"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_prenom"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_nom"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_datenais"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_lieunais"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_sexe"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_taille"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_datedeliv"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_dateexp"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_centreenreg"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cin_adresse"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cel_numelec"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cel_region"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cel_depart"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cel_arrond"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cel_com"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cel_lieuvote"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cel_burvote"],"msg":"field required","type":"value_error.missing"},{"loc":["body","cel_nin"],"msg":"field required","type":"value_error.missing"},{"loc":["body","collect_cel_numelec"],"msg":"field required","type":"value_error.missing"}]}
型号
from sqlalchemy import Table, Column
from sqlalchemy.sql.sqltypes import Integer, String, DATETIME, Enum
from config.db import meta
parrain = Table(
'parrain', meta,
Column('id', Integer, primary_key=True),
Column('cin_numcedeao', String(25)),
Column('cin_prenom', String(100)),
Column('cin_nom', String(100)),
Column('cin_datenais', DATETIME),
Column('cin_lieunais', String(100)),
Column('cin_sexe', Enum('F', 'M')),
Column('cin_taille', Integer),
Column('cin_datedeliv', DATETIME),
Column('cin_dateexp', DATETIME),
Column('cin_centreenreg', String(255)),
Column('cin_adresse', String(255)),
Column('cel_numelec', String(20)),
Column('cel_region', String(20)),
Column('cel_depart', String(50)),
Column('cel_arrond', String(50)),
Column('cel_com', String(50)),
Column('cel_lieuvote', String(100)),
Column('cel_burvote', Integer),
Column('cel_nin', String(25)),
Column('collect_cel_numelec', String(20))
)
该架构
from datetime import datetime
from enum import Enum
from pydantic import BaseModel
class TypeEnum(str, Enum):
F: str = 'F'
M: str = 'M'
class Parrainage(BaseModel):
cin_numcedeao: str
cin_prenom: str
cin_nom: str
cin_datenais: datetime
cin_lieunais: str
cin_sexe: TypeEnum
cin_taille: int
cin_datedeliv: datetime
cin_dateexp: datetime
cin_centreenreg: str
cin_adresse: str
cel_numelec: str
cel_region: str
cel_depart: str
cel_arrond: str
cel_com: str
cel_lieuvote: str
cel_burvote: int
cel_nin: str
collect_cel_numelec: str
class Config:
orm_mode = True
POST方法
@parrainage.post("/parrain")
async def write_data(leparrain: Parrainage):
conn.execute(parrain.insert().values(
cin_numcedeao=leparrain.cin_numcedeao,
cin_prenom=leparrain.cin_prenom,
cin_nom=leparrain.cin_nom,
cin_datenais=leparrain.cin_datenais,
cin_lieunais=leparrain.cin_lieunais,
cin_sexe=leparrain.cin_sexe,
cin_taille=leparrain.cin_taille,
cin_datedeliv=leparrain.cin_datedeliv,
cin_dateexp=leparrain.cin_dateexp,
cin_centreenreg=leparrain.cin_centreenreg,
cin_adresse=leparrain.cin_adresse,
cel_numelec=leparrain.cel_numelec,
cel_region=leparrain.cel_region,
cel_depart=leparrain.cel_depart,
cel_arrond=leparrain.cel_arrond,
cel_com=leparrain.cel_com,
cel_lieuvote=leparrain.cel_lieuvote,
cel_burvote=leparrain.cel_burvote,
cel_nin=leparrain.cel_nin,
collect_cel_numelec=leparrain.collect_cel_numelec
))
return conn.execute(parrain.select()).fetchall()
我的PHP客户端
<?php
$url = "http://127.0.0.1:8000/parrain";
$data_array =array(
"cin_numcedeao" => str_replace(' ', '',$_POST["choice_cin_numcedeao"]),
"cin_prenom" => $_POST["choice_cin_prenom"],
"cin_nom" => $_POST["choice_cin_nom"],
"cin_datenais" => $_POST["choice_cin_datenais"],
"cin_lieunais" => $_POST["choice_cin_lieunais"],
"cin_sexe" => $_POST["choice_cin_sexe"],
"cin_taille" => intval($_POST["choice_cin_taille"]),
"cin_datedeliv" => $_POST["choice_cin_datedeliv"],
"cin_dateexp" => $_POST["choice_cin_dateexp"],
"cin_centreenreg" => $_POST["choice_cin_centreenreg"],
"cin_adresse" => $_POST["choice_cin_adresse"],
"cel_numelec" => $_POST["choice_cel_numelec"],
"cel_region" => $_POST["choice_cel_region"],
"cel_depart" => $_POST["choice_cel_depart"],
"cel_arrond" => $_POST["choice_cel_arrond"],
"cel_com" => $_POST["choice_cel_com"],
"cel_lieuvote" => $_POST["choice_cel_lieuvote"],
"cel_burvote" => intval($_POST["choice_cel_burvote"]),
"cel_nin" => str_replace(' ', '',$_POST["choice_cel_nin"]),
"collect_cel_numelec" =>'11111111' //$_POST["collect_cel_numelec"]
);
$data = http_build_query($data_array);
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
header('Content-Type: text/html');
$resp = curl_exec($ch);
if($e = curl_error($ch)){
var_dump($e);
}
else{
$decoded = json_decode($resp); // return an object
foreach($decoded as $key => $val) {
//echo $key . ':' . $val . '<br>';
var_dump($key);
var_dump($val);
}
}
curl_close($ch);
?>
1条答案
按热度按时间pexxcrt21#
谢谢你,这个帖子回答了我的问题。现在它的工作。