Search code examples
javascriptangularjsangular-resource

URL param passed as body param with Angular Resource


I am using Angular (1) with $resource. One of my API request is failing because a URL param is passed as a request body param instead. Why is this happening?

/** @ngInject */
module.exports = function ($resource, API_URL) {
  return $resource(`${API_URL}/users`, null, {
    deactivateUser: {
      url: `${API_URL}/users/deactivate/:id`,
      method: 'put'
    },
    ...
  })
}

Used here:

User.deactivateUser({id})

Solution

  • According to the docs, you should specify {id: '@id'} as a second parameter to $resource function. For non-GET requests this will take id parameter and place it into url string, not request body.

    Here is the fiddle for that.