Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access HTTP status code on successful Restangular request

How can I access the HTTP response status code after a successful request using Restangular?

CoffeeScript Example

Restangular.all('orders').getList().then (result) ->
    console.log result  # no status code
    console.log 'Preferably, status code should be here: ', result.status
, (error) ->
    console.log 'Error status code: ', error.status  # this works

I tried to implement a response extractor to tack it on as metadata, but the status code is already stripped by the time it flows into the extractor.

like image 373
Petrus Theron Avatar asked Jan 27 '14 10:01

Petrus Theron


1 Answers

You would use setFullResponse in your modules config to grab the status code on a successful request.

https://github.com/mgonto/restangular#setfullresponse

var app = angular.module('app', ['restangular']);

// Change setFullResponse to be true in the modules config.
app.config(['RestangularProvider', function (RestangularProvider) {
    RestangularProvider.setFullResponse(true);
}]);

// Using the new response format.
app.controller('someController', ['Restangular', function (Restangular) {
    Restangular.all('orders').getList().then(function (result) {
        // Response from the server.
        console.log(result.data);

        // Response status code.
        console.log(result.status);
    });
}]);

Coffeescriptified:

app = angular.module('app', ['restangular'])

// Change setFullResponse to be true in the modules config.
app.config ['RestangularProvider', (RestangularProvider) ->
    RestangularProvider.setFullResponse true
]

// Using the new response format.
app.controller 'someController', ['Restangular', (Restangular) ->
    Restangular.all('orders').getList().then (result) ->
        // Response from the server.
        console.log result.data

        // Response status code.
        console.log result.status
]

Hope this helps!

like image 108
JDWardle Avatar answered Sep 17 '22 02:09

JDWardle