python hug api返回自定义http cod

2024-05-29 10:58:13 发布

您现在位置:Python中文网/ 问答频道 /正文

在找了一段时间并询问: https://gitter.im/timothycrosley/hug 我找不到解决办法。在

我要寻找的是一种返回自定义http代码的方法,比如说204,以防在get端点满足一个条件。 路由问题的解释是here

但我似乎找不到一种方法来返回除了200以外的任何其他代码


Tags: 方法代码httpshttp路由gethere条件
3条回答

在他们的回购中找到了一个例子(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

基于jbasko的答案,hugs似乎希望status是HTTP状态代码的文本表示。在

例如:

>> print (HTTP_400)
Bad Request

因此,对于更完整的示例,您可以使用所有回答的dict:

^{pr2}$

我已经编译了所有状态代码的列表in this gist

可以引发falcon HTTPError,例如:

raise HTTPInternalServerError

查看更多详细信息:https://falcon.readthedocs.io/en/stable/api/errors.html

相关问题 更多 >

    热门问题