Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add catch_all_405s or ability to define custom error handler for 405 MethodNotAllowed Error

See original GitHub issue

Errors 404 and 405 are treated in a special way by Flask-Restful (see _should_use_fr_error_handler()).

In case of 404 NotFound, we can set the catch_all_404s=False flag, so that Flask’s error handle will deal with the 404 NotFound exception.

There is no equivalent feature for 405 MethodNotAllowed. And so every time a request hits 405, the response will be {'message': 'The method is not allowed for the requested URL.'}. And that is even after registering custom error handler to Flask object: app.register_error_handler(Exception, my_custom_error_handler)

It would be really useful to be able to set catch_all_405s=False, or ideally be able to define our own error handler for 405 error.

Currently the only way to specify custom 405 error is by defining a non-dynamic dictionary: e.g.

    'MethodNotAllowed': {
        'type': 'MethodNotAllowed',
        'description': 'The method is not allowed for the requested URL.',
        'url': None,
        'data': None,
        'status': 405

This however won’t allow us to update the dict elements such as ‘url’ in the given example.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

jakubczaplickicommented, Jul 13, 2018

2 years later and I’ve hit this problem again. For those who may need this, here’s the solution.

If you want to replace the flask-restful 405 error message from

{"message": "The method is not allowed for the requested URL."}

to your custom message generated by your own handler , e.g.

 "description":"The method is not allowed for the requested URL.",

you need to use Flask’s got_request_exception to catch when an exception happens during request processing:

from flask import Flask, got_request_exception
from app.errors import custom_api_error_handler

def create_app(config_name='default'):
  app = Flask(__name__)
  got_request_exception.connect(custom_api_error_handler, app)
 # the rest of your code

from werkzeug.exceptions import HTTPException

def custom_api_error_handler(exception):
  payload = {
    'type': 'InternalServerError',
    'description': 'Internal Error',
    'method': request.method,
    'url': request.url
  if isinstance(exception, HTTPException):
    payload['type'] = 'HTTPException'
    payload['description'] = exception.description
    status_code = exception.code
    payload['description'] = exception.args
    status_code = 500
  return jsonify(payload), status_code
muteebakramcommented, Apr 7, 2021

Thank you for this 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix the HTTP 405 Method Not Allowed Error - Kinsta®
It's an HTTP response status code that indicates that the request method is known by the server but is not supported by the...
Read more >
How to handle "405 error, Method Not Allowed." without an ...
405 Method not allowed (it's ok for me). Code: try { var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(_endPointUri); myHttpWebRequest.
Read more >
How to Fix HTTP Error 405 Method Not Allowed - Hostinger
How to Fix HTTP Error 405 Method Not Allowed · 1. Check the URL · 2. Revert Recent Updates · 3. Check for...
Read more >
405 Method Not Allowed - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, ...
Read more >
405 Method Not Allowed: What It Is and How to Fix It
An overview of what a 405 Method Not Allowed response is, including troubleshooting tips to help you resolve this error in your own ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found