Change abort() calls to JSON responses

This makes the responses more consistent. Now, all responses are JSON.
This commit is contained in:
BBaoVanC 2020-09-05 15:43:36 -05:00
parent 805e545b39
commit b8b5a2518c
No known key found for this signature in database
GPG Key ID: 6D74C8B0E7D791C2
1 changed files with 5 additions and 5 deletions

View File

@ -4,7 +4,7 @@ imgupload.py
Flask application for processing images uploaded through POST requests. Flask application for processing images uploaded through POST requests.
""" """
from flask import Flask, request, jsonify, abort, Response from flask import Flask, request, jsonify, Response
from flask_api import status from flask_api import status
from pathlib import Path from pathlib import Path
import os import os
@ -81,21 +81,21 @@ def upload():
else: # if the extension was invalid else: # if the extension was invalid
print("Uploaded extension is invalid!") print("Uploaded extension is invalid!")
abort(415) return jsonify({'status': 'error', 'error': 'INVALID_EXTENSION'}), status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
else: # if the key was not valid else: # if the key was not valid
print("Key is invalid!") print("Key is invalid!")
print("Request key: {0}".format(request.form["uploadKey"])) print("Request key: {0}".format(request.form["uploadKey"]))
abort(401) return jsonify({'status': 'error', 'error': 'UNAUTHORIZED'}), status.HTTP_401_UNAUTHORIZED
else: # if uploadKey was not found in request body else: # if uploadKey was not found in request body
print("No uploadKey found in request!") print("No uploadKey found in request!")
abort(401) return jsonify({'status': 'error', 'error': 'UNAUTHORIZED'}), status.HTTP_401_UNAUTHORIZED
else: # if the request method wasn't post else: # if the request method wasn't post
print("Request method was not POST!") print("Request method was not POST!")
abort(405) return jsonify({'status': 'error', 'error': 'METHOD_NOT_ALLOWED'}), status.HTTP_405_METHOD_NOT_ALLOWED
if __name__ == "__main__": if __name__ == "__main__":
print("Run with `flask` or a WSGI server!") print("Run with `flask` or a WSGI server!")