Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

flask return error response

@app.route('/')
def index():
    resp = make_response("Record not found", 400)
    resp.headers['X-Something'] = 'A value'
    return resp
Comment

flask error

from flask import Flask, abort
from auth import AuthError
# depending on the error either 400 or any
#just work with the following

@app.errorhandler(404)
def resource_not_found(error):
  return jsonify({
    "success":  True,
    "error": 404,
    "message": "Resource not found"
  }), 404
##This works for any type of status code error
#You'll follow the same steps just change the error value and message :)

### also for Auth error. 

@app.errorhandler(AuthError)
def AuthError(error):
  """Need to return JSON and we'll have to get a response""" 
  response = jsonify(error)
  response.status_code = error.status_code
  
  return response
Comment

flask error

from waitress import serve
# app.run(host='0.0.0.0', port=port) # <---- REMOVE THIS
# serve your flask app with waitress, instead of running it directly.
serve(app, host='0.0.0.0', port=port) # <---- ADD THIS
Comment

PREVIOUS NEXT
Code Example
Python :: date object into date format python 
Python :: how to change turtle shape in python 
Python :: matplotlib logarithmic scale 
Python :: SciPy 1D Interpolation 
Python :: python run command and read output 
Python :: python for loop counter 
Python :: what should you call a decimal value in python 
Python :: Python Tkinter Canvas Widget 
Python :: django template for loop 
Python :: infix to postfix python code 
Python :: replace value in df 
Python :: CSRF verification failed. Request aborted. 
Python :: python get attributes of class 
Python :: isistance exmaple 
Python :: for each loop python 3 
Python :: python list to string 
Python :: get sum from x to y in python 
Python :: how to change avatar of a bot using discord.py 
Python :: Reverse an string Using Recursion in Python 
Python :: pd df drop columns 
Python :: how to fetch all chars of a string before a space in python 
Python :: correlation analysis of dataframe python 
Python :: find where df series is null and print 
Python :: df groupby loop 
Python :: in pandas how to start an index from a specific number 
Python :: run multiple function with multiprocessing python 
Python :: python numpy vstack 
Python :: pandas convert entries in a column after groupby in list 
Python :: import time in python 
Python :: iso date convert in python 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =