Is there a way to set Angular to automatically send requests as x-www-form-urlencoded
instead of JSON by default?
Angular version 1.4.5
I've tried the following code snippet but it doesn't seem to 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 adding this additional code snippet afterwards:
angular.module('APP').run(['$http', '$httpParamSerializerJQLike', function($http, $httpParamSerializerJQLike) {
$http.defaults.paramSerializer = $httpParamSerializerJQLike;
}]);
However, I found that explicitly specifying x-www-form-urlencoded in the service/controller when making requests does work:
$http.post('/auth', $httpParamSerializerJQLike({email: email, pwd: pwd}))
.then(...
Currently, I am using an interceptor to achieve the desired outcome (although I know it's not the best practice):
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 the entire app to use x-www-form-urlencoded without needing extra interceptors like $.param
?