[Fixed]-Create a unit test using Django's test client post method passing parameters and requesting JSON from rest_framework

1👍

Turns out the AJAX data is supposed to appear in request.data, and I was using the wrong approach to submit the data via AJAX from the browser. Django rest_framework (DRF) assumes that data from the request will be passed in the same format as data returned to the client – in this case JSON both ways. As it assumes that for an Accept=application/json request, incoming data will be in JSON format, it automatically parses it and populates request.data for you, and request.GET and request.POST are empty by the time the request reaches your DRF view.

To pass a form of data in the AJAX request I use the jquery form plugin’s .formSerialize() method.

I did just have a .map() compile a dictionary from a form, but this won’t work for radios and other instances where you might have several values for a single key/form id.

Credit for this answer should really go to @dhke, who pointed out my fundamental error. Although perhaps this question should be deleted.

👤Chris

Leave a comment