Maaz Zindani

Loading the index…

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

A Dio request in Flutter fails with HTTP 405 Method Not Allowed

ErrorDioError [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.

  • 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

    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 '{}'
  2. 2Match what you see, then jump to its fix

    1. 1The 405 response carries an Allow header such as Allow: GET, and your code calls postThe method does not match what the route acceptsFix
    2. 2The 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

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 1

Confirmed by rfc-editor.org

Did this fix it?

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 1

Confirmed by rfc-editor.org

Did this fix it?

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:

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.