Python 如何在烧瓶中获取http标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29386995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:31:08  来源:igfitidea点击:

How to get http headers in flask?

pythonhttpflaskhttp-headersauthorization

提问by emil

I am newbie to python and using Python Flask and generating REST API service.

我是 python 的新手,使用 Python Flask 并生成 REST API 服务。

I want to check authorization header which is sent the client.

我想检查发送给客户端的授权标头。

But I can't find way to get HTTP header in flask.

但是我找不到在烧瓶中获取 HTTP 标头的方法。

Any help for getting HTTP header authorization is appreciated.

感谢获得 HTTP 标头授权的任何帮助。

采纳答案by sanyi

from flask import request
request.headers.get('your-header-name')

request.headersbehaves like a dictionary, so you can also get your header like you would with any dictionary:

request.headers行为类似于字典,因此您也可以像使用任何字典一样获取标题:

request.headers['your-header-name']

回答by cieunteung

just note, The different between the methods are, if the header is not exist

请注意,方法之间的不同在于,如果标题不存在

request.headers.get('your-header-name')

will return Noneor no exception, so you can use it like

将返回None或没有异常,因此您可以像这样使用它

if request.headers.get('your-header-name'):
    ....

but the following will throw an error

但以下将引发错误

if request.headers['your-header-name'] # KeyError: 'your-header-name'
    ....

You can handle it by

你可以通过

if 'your-header-name' in request.headers:
   customHeader = request.headers['your-header-name']
   ....

回答by iam.Carrot

If any one's trying to fetch all headers that were passed then just simply use:

如果有人试图获取所有传递的标头,那么只需使用:

dict(request.headers)

it gives you all the headers in a dict from which you can actually do whatever ops you want to. In my use case I had to forward all headers to another API since the python API was a proxy

它为您提供了一个字典中的所有标题,您实际上可以从中执行任何您想做的操作。在我的用例中,我不得不将所有标头转发到另一个 API,因为 python API 是一个代理

回答by Ajeet Verma

Let's see how we get the params, headers and body in Flask. I'm gonna explain with the help of postman.

让我们看看我们如何在 Flask 中获取参数、标题和正文。我要在邮递员的帮助下解释。

enter image description here

在此处输入图片说明

The params keys and values are reflected in the API endpoint. for example key1 and key2 in the endpoint : https://127.0.0.1/upload?key1=value1&key2=value2

params 键和值反映在 API 端点中。例如端点中的 key1 和 key2 : https://127.0.0.1/uploadKEY1=值1&KEY2=值2

from flask import Flask, request
app = Flask(__name__)

@app.route('/upload')
def upload():

  key_1 = request.args.get('key1')
  key_2 = request.args.get('key2')
  print(key_1)
  #--> value1
  print(key_2)
  #--> value2

After params, let's now see how to get the headers:

在 params 之后,让我们现在看看如何获​​取标题

enter image description here

在此处输入图片说明

  header_1 = request.headers.get('header1')
  header_2 = request.headers.get('header2')
  print(header_1)
  #--> header_value1
  print(header_2)
  #--> header_value2

Now let's see how to get the body

现在让我们看看如何获​​得身体

enter image description here

在此处输入图片说明

  file_name = request.files['file'].filename
  ref_id = request.form['referenceId']
  print(ref_id)
  #--> WWB9838yb3r47484

so we fetch the uploaded files with request.files and text with request.form

所以我们用 request.files 获取上传的文件,用 request.form 获取文本