Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

819
Vistas
Cannot return 404 error as json instead of html from a Flask-Restful app

I am working on a simple Flask REST API test and when I call the {{url}}/items for example I get the items list. However if a call is passed to an endpoint that does not exist for example {{url}}/itemsss then I get the error 404 in html.

I would like to make the error handling more friendly and return json instead of html for certain errors such as 400, 404,405...

For the 404 for example i tried this:

@app.errorhandler(404)
def not_found(e):
    response = jsonify({'status': 404,'error': 'not found',
                        'message': 'invalid resource URI'})
    response.status_code = 404
    return response

However it does not work.

My issue is similar to this one: Python Flask - Both json and html 404 error

I wanted to know, if using the blueprints the only way to accomplish this?

If there a simpler way to output the 404 error as json?

For example instead of this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<title>404 Not Found</title>

<h1>Not Found</h1>

<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>

Something like this:

{

error: true,

status: 404,

code: "error.notFound",

message: "API endpoint not found",

data: { }

}

I appreciate your help with this.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Usually when I need to return a custom error message with Flask-RESTful I would do something like:

from flask import make_response, jsonify

def custom_error(message, status_code): 
    return make_response(jsonify(message), status_code)
over 4 years ago · Santiago Trujillo Denunciar

0

I think I found the solution in the official documentation:

from flask import json
from werkzeug.exceptions import HTTPException

@app.errorhandler(HTTPException)
def handle_exception(e):
    """Return JSON instead of HTML for HTTP errors."""
    # start with the correct headers and status code from the error
    response = e.get_response()
    # replace the body with JSON
    response.data = json.dumps({
        "code": e.code,
        "name": e.name,
        "description": e.description,
    })
    response.content_type = "application/json"
    return response
over 4 years ago · Santiago Trujillo Denunciar

0

Code and description are there in HTTP anyways, need only the message. And using jsonify from flask.

from flask import jsonify
from werkzeug.exceptions import HTTPException

@app.errorhandler(HTTPException)
def handle_exception(e):
    return jsonify({"message": e.description}), e.code
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda