0

When using $resource with POST method (the item i want to delete on the server is of type "Todo") My java SpringREST back controller looks like :

/**
 * POST : delete an todo : /todos/DEL
 */
@RequestMapping(value = "/DEL", method = RequestMethod.POST)
public void deletePOST(@RequestBody Todo todo) {
    todoRepository.delete(todo);
}

Angular code :

 vm.deleteTodoRes2 = function deleteTodoRes2(todo) {
                console.log(todo);
                var url = REST_SERVER_URL + "/todos/DEL";
                var urlParamDefaults = {};
                var action = {
                    removeResource: {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        isArray: false,
                        transformRequest : function(data, headers) {
                            console.log("data");
                            console.log(data);
                            var out = angular.toJson(data);
                            console.log("out ");
                            console.log(out);
                            return  out;
                        }
                    }
                };
                var rest = $resource(url, urlParamDefaults, action);
                // API : rest.<method> ( [param],
                //                       if NON GET [entityData],
                //                      [success(value, responseHeaders)], [error(httpResponse)] )
                var param = {};
                rest.removeResource(param, todo,  successCallback, errorCallback);
            };

It works perfectlt with POST, Now if i try changing the http method from POST to DELETE, it does not work anymore.

Spring :

 @RequestMapping(value = "/DEL", method = RequestMethod.DELETE)  <<< only change in JAVA
In JS

var action = {  removeResource: { method: 'DELETE ', <<< only change in JS

It does NOT WORK , the console.log(data); => undefined It seems the Angular does NOT want to take my "toto" data and serialize it in json, why ?

Gauthier Peel
  • 1,438
  • 2
  • 17
  • 35
  • Many libraries ignore body content if DELETE is used http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request – Chandermani Jun 23 '15 at 16:42
  • maybe, but with $http it works perfectly using the same endpoint. With $resource the response does not have any payload. – Gauthier Peel Jun 23 '15 at 18:55

0 Answers0