无法使用 Pytest、Jenkins 和 Flask 进行 POST 调用
Can't do a POST call with Pytest, Jenkins and Flask
我无法使用烧瓶服务创建 post 调用。这是我的代码:
def test_post_create_file(client):
fileContent = {'filename': 'TestName', 'content': 'TestContent'}
client.post('/v1.0/files', data = json.dumps(fileContent))
POST函数:
@app.route('/v1.0/files',methods=['POST'])
def read_create_file():
cont = request.get_json(silent=True)
file = cont['filename']
content = cont['content']
if not file or not content:
return "empty file or content", 404
if create_file(file, content):
return "CREATED", 200
异常:
Error Message
TypeError: 'NoneType' object is unsubscriptable
Trace
client = <FlaskClient <Flask 'e'>>
def test_post_create_file(client):
fileContent = {'filename': 'ArchivoPrueba', 'content': 'ContenidoPrueba'}
> client.post('/v1.0/files', data = json.dumps(fileContent))
test_e.py:24:
我认为这与尝试以数组形式访问 json 对象有关,但我不确定。
刚刚找到答案:
def test_post_create_file(client):
fileContent = {'filename': 'ArchivoPrueba', 'content': 'ContenidoPrueba'}
client.post('/v1.0/files', data = json.dumps(fileContent), content_type='application/json')
缺少 content_type 参数。
我无法使用烧瓶服务创建 post 调用。这是我的代码:
def test_post_create_file(client):
fileContent = {'filename': 'TestName', 'content': 'TestContent'}
client.post('/v1.0/files', data = json.dumps(fileContent))
POST函数:
@app.route('/v1.0/files',methods=['POST'])
def read_create_file():
cont = request.get_json(silent=True)
file = cont['filename']
content = cont['content']
if not file or not content:
return "empty file or content", 404
if create_file(file, content):
return "CREATED", 200
异常:
Error Message
TypeError: 'NoneType' object is unsubscriptable
Trace
client = <FlaskClient <Flask 'e'>>
def test_post_create_file(client):
fileContent = {'filename': 'ArchivoPrueba', 'content': 'ContenidoPrueba'}
> client.post('/v1.0/files', data = json.dumps(fileContent))
test_e.py:24:
我认为这与尝试以数组形式访问 json 对象有关,但我不确定。
刚刚找到答案:
def test_post_create_file(client):
fileContent = {'filename': 'ArchivoPrueba', 'content': 'ContenidoPrueba'}
client.post('/v1.0/files', data = json.dumps(fileContent), content_type='application/json')
缺少 content_type 参数。