How to send data via POST or GET in Mod_Python?

As pointed by Grisha (mod_python's author) in a private communication, here is the reason why application/json is not supported and outputs a "HTTP 501 Not implemented" error:

https://github.com/grisha/mod_python/blob/master/lib/python/mod_python/util.py#L284

The solution is either to modify this, or to use a regular application/x-www-form-urlencoded encoding, or to use something else than the mod_python.publisher handler.

Example with mod_python and PythonHandler mod_python.publisher:

<script type="text/javascript">
var data = JSON.stringify([1, 2, 3, '&=test', "jkl", {'foo': 'bar'}]); // the data to send
xhr = new XMLHttpRequest();
xhr.open("POST", "testjson.py");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function(res) { console.log(xhr.responseText); };
xhr.send('data=' + encodeURIComponent(data));
</script>

Server-side:

import json
from mod_python import apache 

def index(req):
    data = json.loads(req.form['data'])
    x = data[-1]['foo']
    req.write("value: " + x)

Output:

value: bar

Success!