如何在Flask中获取HTTP标头?

问题:如何在Flask中获取HTTP标头?

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

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

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

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

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

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

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

Any help for getting HTTP header authorization is appreciated.


回答 0

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

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

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

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

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

回答 1

请注意,如果标头不存在,则方法之间的区别是

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

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

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

但是以下内容将引发错误

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

你可以通过

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

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

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

will return None or no exception, so you can use it like

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']
   ....

回答 2

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

dict(request.headers)

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

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


回答 3

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

参数项和值反映在API端点中。例如端点中的key1和key2:https : //127.0.0.1/uploadkey1 = value1&key2 = value2

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

在参数之后,让我们看看如何获​​取标题

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

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

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

因此我们使用request.files获取上传的文件,并使用request.form获取文本

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

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

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:

  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

  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