2

My REST API is configured to accept this kind of request

public function lockUserAction($slug)
{} // "lock_user"     [PATCH] /users/{slug}/lock

So sending a patch request to

/api/users/2/lock

Will lock the user with id=2. This is my rest service inside angular.js

angular.module('UserService',['ngResource']).factory('User', function($resource){
    var User = $resource('/api/users/:id',
        {},
        { 
          list: { method: 'GET' },
          lock: { method: 'PATCH' }
        }
    );   
    return User;
});

List works just finde, but lock does not work. The console prints:

PATCH /api/users 405 (Method Not Allowed) 

I invoke it like this

$scope.lock = function(user){
    user.$lock();
}

In the error message I see the url /api/users instead of /api/users/2/lock. Is this normal behaviour? Of course list excepts only GET requests and PATCH requests are not allowed on /api/users only on /api/users/{slug}/lock.

Any ideas why /api/users is called and not /api/users/{slug}/lock. Any ideas how to fix this?

4
  • Patch? You're making up your own HTTP verbs? That's fine, but see what's happening on your server side. For some reason it's returning a 405 error for these requests. Make sure the incoming URL has the :id param in it for one thing. Commented Apr 13, 2013 at 22:49
  • 1
    @darkporter tools.ietf.org/html/rfc5789 Commented Apr 13, 2013 at 22:55
  • 1
    @darkporter I do not make my own HTTP verbs. I use FOSRestBundle in backend which uses PATCH. Yes, but it seems that the wrong URL is called. Commented Apr 13, 2013 at 23:15
  • 1
    never seen PATCH used for ajax requests... inspect actual request in browser console or fiddler and see what method is being sent, and what url is being used Commented Apr 14, 2013 at 0:07

1 Answer 1

4

When you call $lock angular has no idea that you intend to call the same url as before, with some extra path element. Also there is no way for angular to know what is a value of :id param.

Than let angular know about locking:

var User = $resource('/api/users/:id/:action', //add param to the url
        {}, 
        { 
          list: { method: 'GET' },
          lock: { method: 'PATCH',params:{action:"lock",id:"@id"}} //set param value, so angular knows where to send request, @id refers to user id in json representation of user
        }
    ); 
Sign up to request clarification or add additional context in comments.

2 Comments

You answer before the edit worked partially, it was missing the action parameter in the called url (/api/users/2 405 (Method Not Allowed) ). Your current answer has the same problem like in my question. I get again /api/users 405 (Method Not Allowed)
Sorry, I had one more typo , instead of parameters there should be a param key, I've tried the code and it should be working. The only problem can be with '@id' value, because I'm not sure what is the name of property in your user project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.