Thanks to efeakaroz13's hint I adjusted my code to the following working solution.
Javascript:
function updateSourceState(sourceTypeId, sourceId, dataForUpdate) {
$.ajax({
url: '/update',
data: {source_type_id: sourceTypeId, source_id: sourceId, data_for_update: JSON.stringify(dataForUpdate)},
method: 'PUT',
success: function (data) {
console.log('Data updated successfully');
},
error: function(data) {
console.log('Data update failed. Response: ' + data);
}
}).done(function() {
console.log('DONE: data updated successfully');
}).fail(function(msg) {
console.log('FAIL: data update failed: ' + msg);
}).always(function(msg) {
console.log('ALWAYS message: ' + dataForUpdate + ', url: ' + url);
});
}
Python/Flask "PUT" route:
@bp.route('/update', methods=["GET", "PUT"])
def update_source_state():
source_id = request.form.get('source_id')
source_type = request.form.get('source_type_id')
dataForUpdate = request.form.get('data_for_update')
# build endpoint url (could be done in route annotation too...)
url = '{0}/{1}/source/{2}'.format(ARCHIVE_CONFIG_BASE_URL, source_type, source_id)
response = requests.put( url=url
, data=dataForUpdate
, headers={'Content-Type': 'application/json', 'accept': 'application/json'}
)
...
Note that dataForUpdate
is a JSON object. The code did not work before I stringified it.