How to do a post to flask from a button

Solution
document.getElementById("sendbutton").addEventListener('click', () => {
    var image = canvas.toDataURL();
    var r = new XMLHttpRequest();
    r.open("POST", "http://127.0.0.1:5000/truthMask", true);
    r.onreadystatechange = function () {
        if (r.readyState != 4 || r.status != 200) return;
        //alert("Success: " + r.responseText);
        console.log("sent");
    };
    // Send data in below way from JS
    r.send(JSON.stringify({"input": "test"}));
});
import json

@app.route('/truthMask', methods=['POST'])
def set_truthMask():
    print("Got the data")
    foo = json.loads(request.data.decode())["input"]
    print("Print the data: {}".format(foo))
    return "sent"