Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python hug api return custom http code

Tags:

python

http

api

hug

After looking for it for quite a while and asking on: https://gitter.im/timothycrosley/hug I'm unable to find a solution.

what I'm looking for is a way to return a custom http code, lets say 204 in case a condition is met in the get end point. the explanation for routing problems is here

but I can't seem to find a way to return any other codes other than 200 with a null response

like image 987
Nick Kobishev Avatar asked Jan 03 '17 13:01

Nick Kobishev


2 Answers

Found an example in their repo (https://github.com/berdario/hug/blob/5470661c6f171f1e9da609c3bf67ece21cf6d6eb/examples/return_400.py)

import hug
from falcon import HTTP_400

@hug.get()
def only_positive(positive: int, response):
    if positive < 0:
        response.status = HTTP_400
like image 181
jbasko Avatar answered Oct 04 '22 08:10

jbasko


You can raise the falcon HTTPError, for example:

raise HTTPInternalServerError

see more details: https://falcon.readthedocs.io/en/stable/api/errors.html

like image 30
Ford Guo Avatar answered Oct 04 '22 07:10

Ford Guo