0

$resource is not correctly passing along a url parameter when making a PUT request, via custom action.

This is my service creating the resource.

.factory('cartItemsService', ['$resource', function($resource) {
    return $resource('/api/cart/:cartId/items/', {format: 'json'}, {
        get: {method: 'GET', isArray: true},
        update: {method: 'PUT', isArray: true},
    });
}])

In my controller I'm trying to update the list of items like this. Note that $scope.cart.id exists and is correct (in this case 1)

$scope.cartItems = cartItemsService.update({cartId: $scope.cart.id});

However the request URL is: /api/cart/items/ but I'm expecting /api/cart/1/items/. This works fine if I do .get({cartId: <some_id>}) but doesn't seem to work for update.

EDIT: Angular version 1.1.5

blindworld
  • 121
  • 1
  • 8
Andre
  • 1,292
  • 1
  • 17
  • 34

1 Answers1

0

In the end this was due to the request headers I was setting before making the request.

I was attempting to set put headers like such:

$http.defaults.headers.put['X-CSRFToken'] = $cookies.csrftoken;

This is what caused the request url to be incorrectly formatted.

Changed it to set the post header instead, and it worked.

$http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
Andre
  • 1,292
  • 1
  • 17
  • 34