7

How do I get the response object after I send a Restangular POST?

 firstAccount.post("Buildings", myBuilding).then(function() {
   console.log("Object saved OK");
 }, function() {
  console.log("There was an error saving");
 });

I'm trying to get the new object id.

Thanks.

3 Answers 3

17

I'm the creator of Restangular. Flim is right :).

In the promise then you get the object returned from the server :)

firstAccount.post("Buildings", myBuilding).then(function(addedBuilding) {
   console.log("id", addedBuilding.id);
 }, function() {
  console.log("There was an error saving");
 });

Thanks!

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

7 Comments

are you sure it's the actual response? I have a api endpoint that returns different data than the one sent. on the promisse i'm getting the same data as sent on post, including restangular methods and all.
Hey. Restangular method are indeed added to all server responses. That's part of what makes it great. That all your objects that you receive are enhanced to be used with Restangular. If your post doesn't actually return anything from the server, I'm actually returning the same object you posted so that you get something. So if you're getting exactly the same data you're posting, check that you're returning data from the server OK.
If you need something different in POST vs GET, you can do that using requestInterceptor if it happens for all requests or using customPOST or customPUT methods for sending custom objects, but you can make it work
@mgonto how can I get only the clean response object ? without Restangular wrapped objects ?
@NarekMamikonyan you can call the method .plain() on that "restangularized" object you are getting in the response. See my answer below.
|
2

I haven't worked with Restangular directly, but your POST probably needs to return a JSON object with the ID. Then, your success function has to accept it as a parameter.

firstAccount.post("Buildings", myBuilding).then(function(resp) {
  console.log(resp.id);  // if the JSON obj has the id as part of the response
});

Comments

0

A restangular POST will expect the same object in response than the one posted.

This is clearly seen with typescript definitions. Suppose we have a method that is going to receive an object of type ITypeA and it's going to POST it in a url like http://whatever/api/objects. Suppose that the REST api returns a 201 and also a json with the response object that can be the same OR DIFFERENT. In our case suppose the type returned would be ITypeB. Then our restangular will not be able to use a standard POST of ITypeA and expect a response of ITypeB, so the following code won't be correct because restangular would expected to receive a response of type ITypeA (same as the one posted).

public postAnObject(objectToPost: models.ITypeA): ng.IPromise<models.ITypeB> {
     return this.restangular.all("objects")
            .post<models.ITypeA>(objectToPost)
            .then((responseObject: models.ITypeB) => {
                return responseObject;
            }, (restangularError: any) => {
                throw "Error adding object. Status: " + restangularError.status;
            });
}

That can be resolved by using a customPOST, so the code above would be correct like this:

public postAnObject(objectToPost: models.ITypeA): ng.IPromise<models.ITypeB> {
     return this.restangular.all("objects")
            .customPOST(objectToPost)
            .then((restangularizedObjectTypeB: restangular.IElement) => {
                return restangularizedObjectTypeB.plain();
            }, (restangularError: any) => {
                throw "Error adding object. Status: " + restangularError.status;
            });
}

There are a few things to notice, in summary:

  1. Restangular can get the response object in the successfull callback (the then part)
  2. If using the method .post(objectA) restangular will expect, if any, a successfull callback with a response of the same type as the objectA.
  3. If you want to post an objectA but get a response objectB (different types) then use the method .customPOST(objectA)
  4. IMPORTANT: the response is actually a "restangularized" object that wraps the "real" response object. That means that the response contains some restangular methods. If you simply want the response object call the method .plain() on the response as shown in my second example where the response is not actually a ITypeB object but a restangular.IElement

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.