我怎样才能用 python 拥抱获取请求标头

mon*_*ty0 5 python hug

在注释为拥抱 api 调用的函数中,如何获取该调用的标头?

dan*_*ius 4

简单、正常且最快的方法:Hug 提供requestbody(POST),如果它们作为参数存在的话 ( https://github.com/timothycrosley/hug/issues/120 )。

@hug.get('/headers', output=hug.output_format.json)
def headers(request, header_name: hug.types.text=None):
    if header_name is None:
        return request.headers
    return {header_name: request.get_header(header_name)}
Run Code Online (Sandbox Code Playgroud)