I'm trying to make a flask api with python but I've run into a issue, If i only return a http error code 405
for example the code with fail will the following:
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a int.
I've attached some code to help illustrate the issue. Removing unnecessary stuff.
from flask import Flask, render_template, request
from flask_api import status
@app.route('/weights', methods=['GET','POST'])
def weights():
if request.method == "GET":
return data,status.HTTP_200_OK
if request.method == "POST":
#add to db
return loc,status.HTTP_201_CREATED
return status.HTTP_405_METHOD_NOT_ALLOWED
The issue occurs on the last line,which does indeed return an int 405.
If i use return "junk",status.HTTP_405_METHOD_NOT_ALLOWED
it will work as expected return the 405 and junk.
Is there any reason why I cant just return the response code? Should I always be returning some html message saying method not allowed or whatever my code is.
EDIT: Bolded my question which is not answered in those questions, they just state that you have to send an empty string, I'm asking should that always be the case and why.