A Dio request in Flutter fails with HTTP 405 Method Not Allowed
DioError [DioErrorType.RESPONSE]: Http status error [405]Short answer
405 means the server knows the URL but not the HTTP method you used on it. The server must list the allowed methods in an Allow header, so read that first. If the method is right, check for an http to https redirect, which can make a POST arrive as a GET.
Fix it fast
1Find out which cause you have
Send the same method to the same URL with curl and read the response headers. The Allow header lists the methods the server accepts for that URL.
curl -i -X POST https://api.example.com/items -H "Content-Type: application/json" -d '{}'2Match what you see, then jump to its fix
- 1
The 405 response carries an Allow header such as Allow: GET, and your code calls postThe method does not match what the route acceptsFix - 2
The URL in your app starts with http:// or lacks a trailing slash, and a POST that works in other tools gets 405 from DioA redirect changed your POST into a GETFix
- 1
What does the error look like?
DioError [DioErrorType.RESPONSE]: Http status error [405]405 Method Not Allowed
Cause 1 of 2
The method does not match what the route accepts
1Check
RFC 9110 says the server must send an Allow header with a 405 response, listing the methods the resource supports now. Compare that list with the Dio call you make.
curl -i -X POST https://api.example.com/items
grep -rn "dio\.\(get\|post\|put\|patch\|delete\)" lib/2Fix
Call the method the route allows, or ask the backend owner to enable the method you need on that route.
// route only allows GET
final res = await dio.get('https://api.example.com/items');3Verify
The call returns a 2xx response instead of throwing.
curl -i https://api.example.com/items | head -n 1Confirmed by rfc-editor.org
Cause 2 of 2
A redirect changed your POST into a GET
1Check
RFC 9110 allows a client to change the method from POST to GET when it follows a 301 or 302 redirect. Dio follows redirects by default (followRedirects is true, up to 5), so the final request can be a GET sent to the redirect target.
# a 301/302 with a Location header means the app is being redirected
curl -i -X POST http://api.example.com/items -H "Content-Type: application/json" -d '{}'2Fix
Use the final URL from the Location header, such as the https address, in your base URL. While debugging, turn off redirect following so the redirect shows up as a response instead of a silent method change.
final dio = Dio(BaseOptions(baseUrl: 'https://api.example.com'));
// debugging only: see the redirect itself
await dio.post(
'/items',
data: {'name': 'test'},
options: Options(followRedirects: false, validateStatus: (s) => s != null && s < 400),
);3Verify
The POST goes straight to the final URL and no 301 or 302 appears in curl -i.
curl -i -X POST https://api.example.com/items -H "Content-Type: application/json" -d '{}' | head -n 1Confirmed by rfc-editor.org
How do I stop it coming back?
- Keep the API base URL in one place and always use its final https address.
- Write down the allowed methods per route in the API contract, and let the app call only those.
- Print the response headers of any 4xx response while developing; the Allow header answers most 405 questions.
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
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
Flutter Dio throws a bad response error with status 422
Unhandled Exception: DioError [DioErrorType.response]: Http status error [422]
A 422 is not a Dio fault.
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