user3434888
user3434888

Reputation: 41

AngularJS request.POST return QueryDict: {}

I am making a POST request in AngularJS as follows:

'use strict';
angular.module('app').controller('RegisterController', function($scope, $http) {
         $scope.login = []
         $scope.registrar = function() {

         $http({
            method  : 'POST',
            url     : '/register/',
            headers : {'Content-Type': 'application/json'}, 
            data    : {'username' : $scope.login.username,
                       'password' : $scope.login.password,
                       'email' : $scope.login.email, 
                       'permission' : $scope.login.permission }
          })          
          .success(function(data) {
            $scope.gists = data;
          })
          .error(function(data, status) {
            console.error('Repos error', status, data);
          })
          .finally(function() {
            console.log("finally finished repos");
          });   

        };
})

And in Django I'm calling request.POST["username"] method to get the information.

But information is not returning, I took a print command in request.POST and request.GET to see and both return QueryDict: {}.

I need to do some different configuration for the REST works?

Upvotes: 1

Views: 267

Answers (1)

mariodev
mariodev

Reputation: 15549

It's accessible via request.body so you need to do json.loads(request.body) to retrieve the posted data.

Also consider using DRF instead. It will handle retrieving json payload automatically.

Upvotes: 2

Related Questions