Search code examples
pythonrequestbottle

Get Headers/Cookies from Bottle API request


I was making an API call to a Bottle service and was passing headers in the call using Python's request Library.

requests.get('http://localhost/API/call', headers={"cat":"tax"})

I wanted to get the custom headers passed in the function that gets called through the API call.

Using bottle.request.headers I get the following data: enter image description here

Now, the custom header I passed is present in the environ dictionary with key/value 'HTTP_CAT':tax.

Same thing for cookies. Cookie data can be retrieved using bottle.request.cookies

How can I filter out only the custom header that I am passing in the request?


Solution

  • I'm not sure exactly what you mean by "filter," but the typical way to retrieve request headers from Bottle is with get_header:

    cat_value = request.get_header('cat')
    

    Bottle also has a specific API for retrieving individual cookies. Perhaps there's a good reason you're going down to the raw environ, but if not, then you should be using these built-in methods.

    PS, you may also want to prefix your custom headers with "X-", e.g. X-Cat.