3
在註釋爲hug api調用的函數中,如何獲取該調用的頭文件?如何使用python hug獲取請求頭文件
在註釋爲hug api調用的函數中,如何獲取該調用的頭文件?如何使用python hug獲取請求頭文件
最簡單,普通和最快的方式:擁抱提供request
和body
( 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)}
創建自定義指令[1]:
@hug.directive()
def headers(request=None, **kwargs):
"""Returns the request"""
return request and request.headers
要使用它,在前面加上魔法hug_
前綴:
@hug.post('/sns/test')
def sns_test(hug_headers):
message_type = 'X-AMZ-SNS-MESSAGE-TYPE'
is_subscription = message_type in hug_headers \
and hug_headers[message_type] == 'SubscriptionConfirmation'
return {'is_sub': is_subscription}
無需爲此創建任何指令,hug提供請求(如果作爲參數存在) – danigosa
Thanks @danigosa。我無法從文檔中弄清楚這一點,但它非常合理。 – monty0
使用hug的一個缺點是它的文檔很差,沒有記錄。有時候我認爲他們在顏色,CSS和圖像上花費的時間比代碼示例要多,我希望他們將來能夠改進 – danigosa