How do I send a json POST request using Javascript and read it on a flask server?
Last approach (reference):
const uploadjson = storeDictionary
const url = "/uploadset/"+setName
fetch(url, {method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: uploadjson})
.then((response) => response.json())
.then((data) => {
console.log('Success:', uploadjson);
})
.catch((error) => {
console.error('Error:', error);
console.log("Data:", uploadjson)
});
It results in the following error message:
Here's a part of the python code (flask) I wrote with help from the documentation:
@app.route("/uploadset/<name>", methods=['GET','POST'])
def uploads(name):
if flask.request.method == "POST":
print("Upload!")
if 'id' in flask.session:
if flask.session['id'] in client:
sessid = flask.session['id']
if name != "":
print("flask.request.files", flask.request.files)
file = flask.request.files['file']
try:
if processUploadedSet():
error = False
success = True
This results into a "server didn't understand your request".
I found out that the flask server is the problem:
If I return an empty string on the server the request works 'fine'. How do I read json data using flask which was sent using the javascript?