Python 在测试客户机上设置头不会';好像不行

Python 在测试客户机上设置头不会';好像不行,python,testing,flask,werkzeug,Python,Testing,Flask,Werkzeug,我正在尝试设置标题“授权”:“Bearer foo”。但是,在PyCharm的调试器中调试应用程序时,我在请求中找不到任何显示已设置标头的内容。当我使用邮递员发送请求时,确实会显示标题。如何在Flask的测试客户端中正确发送标头 headers = {'Authorization': 'Bearer foo'} test_response = self.app.post('/api_endpoint', headers=headers, data=dict(foo=bar)) 我无法复制你的问

我正在尝试设置标题
“授权”:“Bearer foo”
。但是,在PyCharm的调试器中调试应用程序时,我在
请求中找不到任何显示已设置标头的内容。当我使用邮递员发送请求时,确实会显示标题。如何在Flask的测试客户端中正确发送标头

headers = {'Authorization': 'Bearer foo'}
test_response = self.app.post('/api_endpoint', headers=headers, data=dict(foo=bar))

我无法复制你的问题。我创建了一个简单的测试,标题设置正确

from flask import Flask, request

app = Flask(__name__)

@app.route('/', methods=['POST'])
def index():
    return request.headers['Authorization']

with app.test_client() as c:
    r = c.post('/', headers={'Authorization': 'Bearer foo'}, data={'foo': 'bar'})
    print(r.data)
运行此命令,输出为
b'Bearer foo'
。在PyCharm的调试窗口中运行时,
request.headers['Authorization']
也已正确设置

Connected to pydev debugger (build 141.1899)
>>> request.headers['Authorization']
Out[1]: 'Bearer foo'