Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter Dio throws a bad response error with status 422

ErrorDioException with type badResponse and status code 422

Short answer

A 422 is not a Dio fault. The server understood the request format but refused the content, usually a failed validation rule. Dio throws because the status is outside its success range. Read the response body in the catch block, then fix the field it names or the content type your request sends.

  • Applies to Flutter 3.35, Dart 3.9, dio 5.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Catch the exception and print what the server said. The body names the field or rule that failed.

    try {
      await dio.post('/users', data: payload);
    } on DioException catch (e) {
      print(e.response?.statusCode);
      print(e.response?.data);
    }
  2. 2Match what you see, then jump to its fix

    1. 1The response body lists a field, such as email or password, and a ruleThe server rejected the values you sentFix
    2. 2The API accepts the same data from a form or a test tool but not from the appThe body is sent in a different format than the API expectsFix
    3. 3The server's 422 body is the answer your screen wants to showYour code needs the 422 body as a normal resultFix

What does the error look like?

  • DioException with type badResponse and status code 422
  • Unhandled Exception: DioError [DioErrorType.response]: Http status error [422]

Cause 1 of 3

The server rejected the values you sent

1Check

RFC 9110 defines 422 as: the server understands the content type and the syntax is valid, but it could not process the instructions in it. Dio reports it as a bad response because 422 is outside the success statuses.

print(e.response?.data);

2Fix

Correct the value the body points to (a missing required field, a wrong format, a duplicate). Fix it in the form or payload, not in Dio.

final response = await dio.post('/users', data: {
  'email': email.trim(),
  'password': password,
});

3Verify

The request returns 2xx and no exception is thrown.

print(response.statusCode);

Confirmed by rfc-editor.org

Did this fix it?

Cause 2 of 3

The body is sent in a different format than the API expects

1Check

By default Dio serialises request data other than a String as JSON. If the API wants a form body, set the content type for the request.

print(e.requestOptions.contentType);

2Fix

Match the API's documented format: send urlencoded form data with the matching content type, or FormData for multipart uploads.

await dio.post(
  '/info',
  data: {'id': 5},
  options: Options(contentType: Headers.formUrlEncodedContentType),
);

3Verify

Resend the request and compare the status with the API docs.

print(response.statusCode);

Confirmed by github.com

Did this fix it?

Cause 3 of 3

Your code needs the 422 body as a normal result

1Check

Dio treats a status as success only when validateStatus says so. The exception type for a rejected status is badResponse.

print(e.type);

2Fix

Either handle the exception and read e.response, or allow 422 through validateStatus so you get the response object.

final r = await dio.post(
  '/users',
  data: payload,
  options: Options(
    validateStatus: (s) => s != null && (s < 300 || s == 422),
  ),
);
if (r.statusCode == 422) { /* show r.data errors */ }

3Verify

A 422 now returns a Response and your code shows the messages.

print(r.data);

Confirmed by pub.dev

Did this fix it?

How do I stop it coming back?

  • Validate fields in the app with the same rules as the API.
  • Always read e.response?.data in the catch block before guessing.
  • Log e.type and e.response?.statusCode for every DioException, so failures are classified the same way everywhere.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Dio POST request ignores the headers you set

The server rejects the body because Content-Type is application/json, not application/x-www-form-urlencoded

Headers belong in Options for one request or in BaseOptions for the whole Dio instance, never in the data map.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.