Search code examples
pythonhug

How can I get request headers with python hug


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


Solution

  • 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)}