I have created a service with RESTAngular in order to ease my communication with my REST API. Let's say that i have an object Person
.
Here is my service:
myApp.factory('RESTService', [ 'Restangular', function (Restangular) {
var restAngular = Restangular.withConfig(function (Configurer) {
Configurer.setBaseUrl('/myAPI/');
});
var service = {};
service.Person= restAngular.service('person');
return service;
}]);
I can successfully:
GET the list of Person
RESTService.Person.getList().then(function (response) {
$scope.persons = response.plain();
})
GET one Person
RESTService.Person.one(id).get().then(function (response) {
$scope.person = response.plain();
})
POST (Update) Person
RESTService.Person.post($scope.person).then(
function (successResponse) {
// success stuff
},
function (errorResponse) {
console.log("fail", errorResponse.status);
}
)
But i can't do PUT
(create a new record). Now let's say i have a form and the form's data is being kept in $scope.formData
. I want to do make a PUT
request passing the object contained in $scope.formData
to my API. How do i do that?
EDIT: For clarification my API is REST, so i do
GET - /myAPI/person
to get the list of all the personsGET - /myAPI/person/123
to get the person with id=123POST - /myAPI/person/123
to update the person with id=123PUT - /myAPI/person
to insert a new person in my databaseWhen creating a person for which you don't know the id yet, your API should accept a POST
on myAPI/person
. I assume Restangular expects that.
You don't want to accept a PUT
, because the request is not idempotent. This is part of the semantics of a PUT
and you want to adhere to that.
This SO question explores this in more detail: PUT vs POST in REST