21

I have a request like this:

$http({ 
    method: 'POST', 
    url: '/url/', 
    data: 'test=data'
})

In my django views:

class SomeClass(View):
    def get(self, request):
        return HttpResponse("Hello")
    def post(self, request):
        print request.post
        print request.body
        return HttpResponse("Done")

So when I do request.POST I get an empty query dict :<QueryDict: {}>

But my request.body has: test=data

So I believe django receives the data as url-encoded parameters and not as a dictionary.

How do I send or receive this data as JSON/Dict ?

2
  • Is the code correct? I think you should use request.POST instead of request.post. Commented Sep 24, 2013 at 10:32
  • Instead of data, try params and give an object instead of string. Ref: docs.angularjs.org/api/ng.$http#parameters Commented Sep 24, 2013 at 11:09

6 Answers 6

44

When calling ajax, you recieve encoded json string in request body, so you need to decode it using python's json module to get python dict:

json.loads(request.body)
Sign up to request clarification or add additional context in comments.

3 Comments

I like this solution because I can use AngularJS as it was designed instead of hacking it to work as I'm used to working with jQuery.
Beautiful idiomatic method.
If you are using angular 4 and DRF use request.data.
14

In my case works something like

$http({
    url: '/url/',
    method: "POST",
    data: $.param(params),
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
})

Or more nice variant:

app.config ($httpProvider) ->
    ...
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

and then

$scope.save_result = $http.post('/url/', $.param(params))

http://www.daveoncode.com/2013/10/17/how-to-make-angularjs-and-django-play-nice-together/

Comments

2

I am using zope2 where I used simplejson to decode the request json into python dictionary as:

request_dict = simplejson.loads(request.get('BODY','')

It's working correctly for me. In this way I am able to use angularjs default json request rather than converting it into form post.

1 Comment

saved the day for me
2

I improved mariodev's solution a bit by creating a decorator:

# Must decode body of angular's JSON post requests
def json_body_decoder(my_func):
    def inner_func(request, *args, **kwargs):
        body = request.body.decode("utf-8")
        request.POST = json.loads(body)
        return my_func(request, *args, **kwargs)
    return inner_func

 @json_body_decoder
 def request_handler(request):
     # request.POST is a dictionary containing the decoded body of the request

Now I just add the @json_body_decoder decorator whenever I create a request handler that deals with post data in application/json.

Comments

1

For angular 4 and Django Rest Framework use request.data to get json object.

like:

posted_data = request.data

Comments

0

The $http service expects a JS object, not a string. Try this:

$http({ 
    method: 'POST', 
    url: '/url/', 
    data: {test: 'data'}
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.