My question is similar with request.form.getlist won't return any entries except that I am sending requests within Python. Simplified codes are written below:
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
gives me {"report_set": []}
and status code 200. How am I supposed to get {"report_set": ["a", "b", "c", "d", "e"]}
from response.content
? (Strangely it worked yesterday... :/)
Edits after being marked as duplicated:
According to How to get POSTed json in Flask? and Post JSON using Python Requests, one can use json
parameter to pass the json contents with a Python dictionary. However I've already tried that before, I just didn't write it previously:
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)
With any one, any two, or even all of data
, json
, headers
parameters being given in requests.post
, I am still getting {"report_set": []}
from response.content
.
Besides, from the answer of How to get data received in Flask request, I believe that using request.form.getlist("images")
is correct. What request.form
is for includes "the key/value pairs in the body, from a HTML post form", and I did send a post request with a key/value pair.
Excuse me but I am not getting help from those suggested duplicates.