'Receiving 422 Error Flutter post request and Fastapi
I am attempting to use flutter on the front to send json data to the backend. I am getting 422 errors which I understand are if the parameter is declared to be of the type of a Pydantic model, it will be interpreted in the request body.
So, following other stackoverflow posts and what I can gather from the docs I tried
Future<void> register() async {
final uri = 'http://localhost:8000/register';
final body = {
"username": unController.text,
"fullname": fullnameController.text,
"email": emailController.text,
"password": passwordController.text,
"disabled": false,
};
var json_string = json.encode(body);
print(json_string);
var response;
try {
response = await http.post(
Uri.parse(uri),
body: json_string,
);
} catch (e) {
print(e);
}
print(response);
My route signature looks like so.
class User(BaseModel):
username: str
fullname: str
email: str
hashed_password: Optional[str]
disabled: bool
password: Optional[str]
@authenticate.post('/register', response_model=User)
async def register_user(new_user: User):
I'm new to flutter and not sure why I am still receiving 422 status code. I keep running into this issue, but I'm unsure as how to proceed.
Solution 1:[1]
I have no clue which lang on code preview on bellow, but maybe you can be forgot add return JSON type of route. Because 422 error meaning error unprocessable entity. If i know which language you use, i can help you more specific method.
Maybe you need to add like return json.Encode(data) things.
Or you can set header "Content-Type": "application/json" :/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kaan Kuscu |
