Answer the question
In order to leave comments, you need to log in
How to correctly send json POST request?
Hello.
Probably a banal question, BUT you still have to ask
it, you need to send JSON
{"id": "1", "context1": "xyz1231sdfsdf"}
which will turn into an UPDATE in the PostgreSQL database
var json_context = {"id": "1", "context1": "xyz1231sdfsdf"}
var xhr = new XMLHttpRequest(); // new HttpRequest instance
var url = 'http://localhost:5000/test_update'
xhr.open("POST", url, true)
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
// alert(xhr.responseText);
console.log(xhr.responseText);
}
};
xhr.send(JSON.stringify(json_context));
console.log(xhr.responseText);
$ curl --header "Content-Type: application/json" --request POST --data '{"id":"1","context1":"xyz123ddfg"}' http://localhost:5000/test_update
@main_app.route('/test_update', methods=['GET', 'POST'])
def test_post():
if request.method == "POST":
print(request.is_json)
content = request.get_json()
print(content)
print(content["id"])
db.session.execute('update test set context1= :value1 where id= :value2',{'value1': content["context1"], 'value2': int(content["id"])})
db.session.commit()
return jsonify(content)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question