Skip to content

Instantly share code, notes, and snippets.

@marcosbondel
Last active May 27, 2025 19:01
Show Gist options
  • Save marcosbondel/bc3b170ff1614e0169799e13ef6bb8a5 to your computer and use it in GitHub Desktop.
Save marcosbondel/bc3b170ff1614e0169799e13ef6bb8a5 to your computer and use it in GitHub Desktop.
Métodos para respuestas JSON de una API en Flask (Python)
from flask import Response
import json
def respond_with_success(data = ''):
return Response(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
def respond_with_error(message = '', details = []):
response = {
'message': message,
'details': details
}
return Response(
response=json.dumps(response),
status=400,
mimetype='application/json'
)
def respond_with_not_found():
return Response(
response=json.dumps('Resource not found'),
status=404,
mimetype='application/json'
)
def respond_with_unauthorized():
return Response(
response=json.dumps('Your are not allowed -_-'),
status=401,
mimetype='application/json'
)
def respond_with_internal_server_error(msg = 'Internal server error'):
return Response(
response=json.dumps(msg),
status=500,
mimetype='application/json'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment