Flutter Dio throws a bad response error with status 422
DioException with type badResponse and status code 422Short 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.
Fix it fast
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); }2Match what you see, then jump to its fix
- 1
The response body lists a field, such as email or password, and a ruleThe server rejected the values you sentFix - 2
The 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
The server's 422 body is the answer your screen wants to showYour code needs the 422 body as a normal resultFix
- 1
What does the error look like?
DioException with type badResponse and status code 422Unhandled 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
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
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
A Dio request in Flutter fails with HTTP 405 Method Not Allowed
405 Method Not Allowed
405 means the server knows the URL but not the HTTP method you used on it.
2 causes, each with a fixMobile apps
Dio throws 'status code of 404 and RequestOptions.validateStatus' in Flutter
This exception was thrown because the response has a status code of 404 and RequestOptions.validateStatus was configured to throw for this status code.
Dio throws a DioException for any response outside 2xx and 304, and a 404 is one.
3 causes, each with a fixMobile apps
Dio requests fail with a connection or receive timeout in Flutter
DioExceptionType.connectionTimeout
Dio throws a DioException whose type says which timer ran out: connectionTimeout, sendTimeout or receiveTimeout.
4 causes, each with a fixMobile apps
Dio interceptor throws: the handler has already been called
Bad state: Future already completed
In Dio every interceptor callback gets a handler, and that handler may be completed only once.
2 causes, each with a fixMobile 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 fixMobile apps
Setting a timeout on an HTTP request in Flutter and Dart
SocketException
Dart HTTP calls have no timeout of their own beyond the operating system's.
3 causes, each with a fix