Last active
May 27, 2025 19:01
-
-
Save marcosbondel/bc3b170ff1614e0169799e13ef6bb8a5 to your computer and use it in GitHub Desktop.
Métodos para respuestas JSON de una API en Flask (Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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