4

I want to angular to make x-www-form-urlencoded requests by default. Not JSON.

angular 1.4.5

This doesnt work.

 angular.module('APP').config(['$httpProvider', function ($httpProvider) {
        $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
        $httpProvider.defaults.headers.post['Content-Type'] =  'application/x-www-form-urlencoded';
        $httpProvider.defaults.paramSerializer = '$httpParamSerializerJQLike';
    }]);

even when I add this after:

angular.module('APP').run(['$http', '$httpParamSerializerJQLike', function($http, $httpParamSerializerJQLike) {
  $http.defaults.paramSerializer = $httpParamSerializerJQLike;
}]);

But works when i use it directly in service/controller when making requests. as:

$http.post('/auth', $httpParamSerializerJQLike({email: email, pwd: pwd}))
.then(...

Now i use This interceptor: (bad code, wrond way, but works as I want)

angular.module('APP').factory('ApiInterceptor', ['$q', '$httpParamSerializerJQLike',
function($q, $httpParamSerializerJQLike){
  return {
    request: function (config) {
      $rootScope.loading = true;
      if (config.method === "POST"){
        if (config.data === undefined) config.data = {};
        config.data = $httpParamSerializerJQLike(config.data);
      }
      return config || $q.when(config);
    }
  };
}]);

How can i configure whole app to use x-www-form-urlencoded without bicycles as $.param interceptors etc.

ubombi
  • 1,115
  • 2
  • 15
  • 30

1 Answers1

1

defaults.paramSerializer only used by URL building, not the POST body, the defaults.transformRequest is what you're looking for

Cotton
  • 1,157
  • 9
  • 16
  • Now i use middleware and json request works great. Thanks, i'll try and ckeck right answer if you are right. – ubombi Oct 26 '15 at 18:23