In a function annotated as a hug api call, how can I get the headers for that call?
The easy, normal and fastest way: Hug provides request
and body
(POST) if they are present as arguments (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)}
Create a custom directive [1]:
@hug.directive()
def headers(request=None, **kwargs):
"""Returns the request"""
return request and request.headers
To use it, prepend the magic hug_
prefix:
@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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With