简体   繁体   中英

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: 在此处输入图片说明

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?

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-", eg X-Cat .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM