IS there a way to do the http method override with angular's $resource
service using the X-HTTP-Method-Override
or a _method
param in the request?
Asked
Active
Viewed 2,319 times
3

rascio
- 8,968
- 19
- 68
- 108
2 Answers
2
In your Resource factory you can specify the method for each type of request.
angular.module('myServices', ['ngResource'])
.factory('Customer', function($resource){
return $resource('../api/index.php/customers/:id', {id:'@id'}, {
update: {method:'PUT'}
});
})
is the standard method, but you could use this too:
angular.module('myServices', ['ngResource'])
.factory('Customer', function($resource){
return $resource('../api/index.php/customers/:id', {id:'@id'}, {
update: {params: {'_method':'PUT', id: '@id'}}
});
})

CubCouper
- 46
- 1
- 7
-
Thank you! I already solved, using a request interceptor to transform the request, in this way I don't need to change the code already written, and I can disable it commenting out the interceptor – rascio Nov 25 '13 at 16:06
1
In case someone else is looking for a code snippet, here it is:
(function(module) {
function httpMethodOverride($q) {
var overriddenMethods = new RegExp('patch|put|delete', 'i');
return {
request: function(config) {
if (overriddenMethods.test(config.method)) {
config.headers = config.headers || {};
config.headers['X-HTTP-Method-Override'] = config.method;
config.method = 'POST';
}
return config || $q.when(config);
}
};
}
module.factory('httpMethodOverride', httpMethodOverride);
module.config(function($httpProvider) {
$httpProvider.interceptors.push('httpMethodOverride');
});
})(angular.module('app-module'));

Darlan Alves
- 503
- 5
- 13