request.form.getlist给出一个空列表

时间:2018-07-04 04:16:23

标签: python python-3.x flask request python-requests

除了在Python中发送请求外,我的问题与request.form.getlist won't return any entries类似。简化代码如下:

app.py

from flask import Flask, request, jsonify
app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/h2ocr", methods=["POST"])
def recognize():  
    report_set = jsonify({"report_set": request.form.getlist("images")})
    return report_set

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7000)

test_app.py

import requests
import json

response = requests.post(
    url="http://localhost:7000/h2ocr",
    data=json.dumps({"images": ["a", "b", "c", "d", "e"]})
)
print(json.loads(response.content), response.status_code)

python test_app.py给了我{"report_set": []}和状态代码200。我应该如何从{"report_set": ["a", "b", "c", "d", "e"]}中获得response.content? (奇怪的是昨天工作了...:/)


在标记为重复后进行编辑:

根据How to get POSTed json in Flask?Post JSON using Python Requests,可以使用json参数通过Python字典传递json内容。但是我之前已经尝试过了,但是之前我没有写过:

import requests
import json

response = requests.post(
    url="http://localhost:7000/h2ocr",
    data=json.dumps({"images": ["a", "b", "c", "d", "e"]}),
    json={"images": ["a", "b", "c", "d", "e"]},
    headers={"Content-Type": "application/json"}
)
print(json.loads(response.content), response.status_code)

data中给定jsonheadersrequests.post参数中的任何一个,甚至两个,我仍然得到{"report_set": []}来自response.content

此外,从How to get data received in Flask request的答案来看,我相信使用request.form.getlist("images")是正确的。 request.form的用途包括“正文中的键/值对,来自HTML发布表单”,我确实发送了带有键/值对的发布请求。

不好意思,但是那些建议的重复项并没有帮助我。

0 个答案:

没有答案