How can I get request headers with python declaration

In a function annotated as a hug api call, how can I get the headers for this call?

+3


source to share


2 answers


Simple, normal and fast way: Hug provides request

and body

(POST) if 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)}

      

+2


source


Create custom directive [ 1 ]:

@hug.directive()
def headers(request=None, **kwargs):
    """Returns the request"""
    return request and request.headers

      



To use it, add the magic prefix 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}

      

+2


source







All Articles