Sajjad javadi
Sajjad javadi

Reputation: 403

Error http 422 and XMLHttpRequest in flutter

I want to connect to a web service. When I use postman, request send and response receive successfully. But in the flutter app, I get error 422 in the android emulator. And with the same code in flutter web, I get XMLHttpRequest error.

My postman:

scrennshot

This is my data that send to the server:

var data = {
  "username": usernameController.text,
  "password": passwordController.text,
  "email": emailController.text
};

And send a request with dio:

Response response = await client
        .post(theUrl,
            options: Options(headers: {
              HttpHeaders.contentTypeHeader: "application/json",
              HttpHeaders.acceptHeader:"*/*"
            }),
            data: jsonEncode(data))
        .timeout(const Duration(seconds: 10));

I get errors on this method:

on DioError catch (error) {
    var statusCode = error.response?.statusCode;
    print("+++++++++++++++" + statusCode.toString());
    print("+++++++++++++++" + error.message);
}

How can I fix these errors?

Upvotes: 0

Views: 460

Answers (1)

Sajjad javadi
Sajjad javadi

Reputation: 403

I Added this code to options in dio and Error 422 in mobile is fixed:

Options(
  validateStatus: (status) {
    return status! < 500;
   },
   followRedirects: false,
   ...)

But I still get error XMLHttpRequest in flutter web.

Upvotes: 1

Related Questions