0

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?

3
  • Maybe... developer.mozilla.org/fr/docs/Web/HTTP/Status/422 Commented Feb 6, 2022 at 20:27
  • Where do you encode the data map into JSON? Commented Feb 7, 2022 at 4:24
  • @RichardHeap Thank you, I edit my code and add jsonEncode to dio. but the error still is 422 in mobile. Commented Feb 7, 2022 at 7:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The web problem will be a CORS issue.
@RichardHeap The server programmer says that CORS is enable on server
Open the Developer Tools tab in chrome - open the Network tab - then perform the request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.