I am trying to find a way to send fields with explicit null values (as opposed to empty string value or the absence of the field itself in the request) via POST method from my UI to my Flask app backend. The latter seems to interpret null values I sent via AJAX as empty strings.
So far I'm sending my AJAX requests from the UI like this:
$.ajax({
type: 'POST',
url: SOME_URL,
data: {
foo: null
},
success: function() {
// process response
}
});
And here's what happens in Flask:
if request.form.get('foo') is None:
# Happens only if 'foo' field was not included in the request
return response('Got None')
elif request.form.get('foo') == '':
# Happens for empty string or null sent as value of 'foo'
return response('Got empty string')
Apparently, it is possible to JSONencode the POST data and subsequently decode it on the server, preserving null values as such. Is there a more elegant way to do that though? For one, using WTForms for validation would become much harder I imagine. And it would seem inconsistent to send some requests JSONencoded and others unencoded.