Search code examples
angularjsrestangularhateoas

Restangular no BaseUrl when do PUT


i'm using Restangular and trying to PUT some data but it seems to lose the BaseUrl. In the config function i define the BaseUrl for Restangular and others Restangular fields.

Constants.restangularBaseUrl is http://192.168.1.100/api/

RestangularProvider.setBaseUrl(Constants.restangularBaseUrl)
    .setRestangularFields({
    selfLink: '_links.self.href',
        id: '_id',
        etag: '_etag'
    })
    .addResponseInterceptor(function(data, operation, what, url, response, deferred){
        if (operation === 'getList') {
            var result = data._items;
            result._meta = data._meta;
            result._links = data._links;
            return result;
        }
        return data;
    });

Then i have some models like this:

(function(){
    angular.module('models.ebayItems', ['services.constants', 'restangular'])
    .service('EbayItems', ['Constants', 'Restangular', function (Constants, Restangular) {
        Restangular.extendModel('ebayitems', function(model) {
            model.toggleMonitor = function(){
                var item = this;
                Restangular.one('ebayitems', this._id).patch({active: this.active}, '', {'If-Match': this._etag})
                .then(function(data){
                    item._etag = data._etag;
                }, function(error){
                    console.log('error', error);
                });
            };
            return model;
        });

        var ebayItems = Restangular.all('ebayitems');

        var ebayItemsOneSearch = function(_id){
            return ebayItems.customGETLIST('', {where: {searchId: _id}});   
        };

        return {
            items: ebayItems,
            oneSearch: ebayItemsOneSearch
        };

    }])
})();

Now when i try to do a put request with an item based on that model:

item.put()

it uses the wrong url, i mean it loses the BaseUrl, so instead of putting at:

http://192.168.1.100/api/ebayitems/12345

it puts at

http://192.168.1.100/ebayitems/12345

resulting in a 404 error.

Why? What am i doing wrong? Any help really appreciated. Thank you


Solution

  • The problem was that setting a selfLink field that was a relative url from the API backend it overrides the BaseUrl. Removing that field from the config function it worked.

    RestangularProvider.setBaseUrl(Constants.restangularBaseUrl)
        .setRestangularFields({
            id: '_id',
            etag: '_etag'
        })
        .addResponseInterceptor(function(data, operation, what, url, response, deferred){
            if (operation === 'getList') {
                var result = data._items;
                result._meta = data._meta;
                result._links = data._links;
                return result;
            }
            return data;
        });