4

I have a backend implemented with ASP Web API and, in angularjs, i have injected restangular for crud. So, in the angularjs controller i have:

gestionale.controller('mainController', function ($scope, $http, Restangular) {

    var basePersonale = Restangular.all('api/Personale');

    basePersonale.getList().then(function (personale) {
        $scope.myData = personale;
    });
....
....
}

At some point in the controller i need to make a PUT request:

var person = $scope.myData[0];
person.put();

The variable "person" is valued correctly but when is executed the put method i see in the Firefox debugger:

Request method: PUT

Request URL: http://127.0.0.1:49375/api/Personale

Status Code: 405 Method not allowed

and the params of the requested is: {"Id":1,"Nome":"Mat"} and is correct.

In effect that method is not allowed because in the web api the put method respond to this url for example:

// PUT api/Personale/5

Why the request url for the PUT method isn't correct?

4
  • I bet it has to do with promise resolution. Try placing your put() call inside of the .then() function block in your above code and see if it works. If it does, then you need to change your code so that the put request is only made once the getList() promise is resolved in a way that works with what you're trying to do. Commented Apr 23, 2014 at 15:39
  • The problem is that i getList i populate a ng-grid and when i edit a cell i need to make a put request Commented Apr 23, 2014 at 16:02
  • If you need to use the object filled by getList(), then you have to make sure put() only fires following the getList() promise resolution. If put() will fire sometimes before or without a getList() object then you need to rework things. Short of that, Restangular will try to operate on your baseUrl as you're seeing. So, you need to either adjust your code to use promises more appropriately or look at the different ways you can build custom URLs using Restangular, avoiding using the object returned by getList() github.com/mgonto/restangular#url-building Commented Apr 23, 2014 at 16:30
  • Method not allowed is a WebAPI error typically indicating that the route in question doesn't support PUT. Mind posting your WebAPI method? Commented Jul 17, 2014 at 3:12

2 Answers 2

3

I was having the exact same issue. I realized that I had no 'id' field in my model. I just made sure to set a id (from the mongodb _id) in my instance and now the put route is correct. In your case, you seem to have an Id field, which should be converted to lowercase.

Sign up to request clarification or add additional context in comments.

Comments

1

If you are using MongoDB you need to tell Restangular that the id field it is expecting is actually _id.

This can be done globally like this:

RestangularProvider.setRestangularFields({
      id: "_id"
    });

Comments

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.