3

I'm creating two POST calls. One using a django form and one using angular js via a resource xhr.

The angular setup looks like this:

myModule.factory('gridData', function($resource) {
    //define resource class
    var root = {{ root.pk }};
    var csrf = '{{ csrf_token }}';
    return $resource('{% url getJSON4SlickGrid root.pk %}:wpID/', {wpID:'@id'},{
            get: {method:'GET', params:{}, isArray:true},
            update:{method:'POST', headers: {'X-CSRFToken' : csrf }}
    });
});

With creating an xhr post request as such:

item.$update();

This post request is send to the server as expected, but when I want to access the QueryDict I cannot access the data passed using:

name = request.POST.get('name', None)

name is always None like this.

The issue behind this is that the QueryDict object is getting parsed quite strange.

 print request.POST
<QueryDict: {u'{"name":"name update","schedule":0"}':[u'']}>

Whereas I would have expected this result, which I got when I send the data via a "normal" Post request:

<QueryDict: {u'name': [u'name update'], u'schedule': [u'0']}>

So it seems to be that Django receives something in the POST request which instructs Django to parse the parameters into one string. Any idea how to circumvent this?

Update:

I found this discussion where they say that the issue is if you provide any content type other than MULTIPART_CONTENT the parameters will be parsed into one string. I checked the content-type send with the POST request and it is really set to 'CONTENT_TYPE': 'application/json;charset=UTF-8'. Thus this is likely the issue. Therefore my question is: How can I set the CONTENT_TYPE for a xhr post request created using angular.js resources to MULTIPART_CONTENT?

Community
  • 1
  • 1
Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177
  • 1
    you may want to look at: http://stackoverflow.com/a/12191613/1238847 – Tosh Oct 11 '12 at 08:53
  • Thanks. I simply set the header content_type as such 2resulting in the correct content type: 'CONTENT_TYPE': 'application/x-www-form-urlencoded'. Unfortunately the params are still transferred in the way I cannot parse them in the view. – Thomas Kremmel Oct 11 '12 at 09:08
  • Possibly http://stackoverflow.com/questions/10520174/seding-data-from-angularjs-to-django – Pratik Mandrekar Oct 11 '12 at 09:13
  • In both of the threads they reference to the $.param function of jquery to send the data correctly to the server. Any idea how to include this $.param function into an angular $resource definition? – Thomas Kremmel Oct 11 '12 at 09:34

1 Answers1

5

you could either:

  • fiddle with the client to send data instead of json
  • use json.loads(request.raw_post_data).get('name', None) (django < 1.4)
  • use json.loads(request.body).get('name', None) (django >= 1.4)

The Angular documentation talks about transforming requests and responses

To override these transformation locally, specify transform functions as transformRequest and/or transformResponse properties of the config object. To globally override the default transforms, override the $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse properties of the $httpProvider.

you can find an example here as was previously pointed at.

Community
  • 1
  • 1
dnozay
  • 23,846
  • 6
  • 82
  • 104
  • it seems to me as META['QUERY_STRING']) contains the URL parameters and not the POST body parameters. Therefore I think it is not a valid option. – Thomas Kremmel Oct 11 '12 at 09:24
  • But when accessing the querydict as you proposed it would work: from django.utils import simplejson as json name = json.loads(request.POST.keys()[0]).get('name', None) . Thanks for this hint. Whereas I m still looking for a method to send the data correctly, as this is more a workaround to me. – Thomas Kremmel Oct 11 '12 at 09:30
  • `request.raw_post_data` can be used as well. – dnozay Oct 11 '12 at 09:59