[英]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. 我正在对Bottle服务进行API调用,并使用Python的请求库在调用中传递
headers
。
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. 我想获得通过API调用调用的函数中传递的自定义标头。
Using bottle.request.headers
I get the following data: 使用
bottle.request.headers
获得以下数据:
Now, the custom header I passed is present in the environ
dictionary with key/value 'HTTP_CAT':tax
. 现在,我通过的自定义标头在
environ
字典中以键/值'HTTP_CAT':tax
。
Same thing for cookies
. cookies
。 Cookie data can be retrieved using bottle.request.cookies
可以使用
bottle.request.cookies
检索Cookie数据
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
: 我不确定“过滤器”到底是什么意思,但是从Bottle检索请求标头的典型方法是使用
get_header
:
cat_value = request.get_header('cat')
Bottle also has a specific API for retrieving individual cookies. Bottle还具有用于检索单个Cookie的特定API。 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
. PS,您可能还想在自定义标头前面加上“ X-”,例如
X-Cat
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.