Search code examples
pythonapihttphug

python hug api return custom http code


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


Solution

  • 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