0

I have problem to proceed HTTP request from Browser with Flutter Web Application

ERROR MESSAGE

BACKEND

127.0.0.1:52159 - "POST /signIn HTTP/1.1" 422 Unprocessable Entity

FRONTEND

ClientException: XMLHttpRequest error., uri=http://127.0.0.1:8000/signIn

My Code

Back-End

@app.post("/signIn", response_model=ApiResponse)
def login(auth: AuthModel):
    response = au.signIn(auth)
    return response
  • AuthModel
  class AuthModel(BaseModel):   
uid: str = None   email: str   password: str   name: str = None

  • ApiResponse

class ApiResponse(BaseModel): message: str description: str = None success: bool status_code: int data: dict = {}

Front-End


Future\<bool?\> signIn() async {
try {
final response = await http.post(
Uri.http(\_url, '/signIn',),
body: authData,
headers: {
"Access-Control-Allow-Origin": "\*"
}
);
print(response.body);
SharedPrefs.setToken(json.decode(response.body)\['data'\]\['token'\]);
return true;
} catch (e) {
print(e);

      return false;
    }

}

Environment INFO

  • Flutter (Channel master, 3.12.0-11.0.pre.12, on macOS 13.4.1 22F82 darwin-x64, locale en-DZ)
  • Python v3.11
  • FastApi v0.100.0
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dzmoxn
  • 1
  • 3
  • Please have a look at [this answer](https://stackoverflow.com/a/70636163/17865804) and [this answer](https://stackoverflow.com/a/75041731/17865804) – Chris Jul 12 '23 at 03:51
  • you need add 'Content-type: application/json' to headers in http – Doan Thai Jul 12 '23 at 08:34

1 Answers1

0

The Problem was solved by following this answer

The problem was that chrome browser have web security rules you need to disable them if your server does not have SSL certificate.

dzmoxn
  • 1
  • 3