0

I'm trying to hit an endpoint that looks like /shop/<item_id>

However, because it also requires an auth token, I am sending an array to my $resource along with the token.
APIshop.create([CreateCampaignService, {auth_token: User.token}]

How can I make it so APIshop is able to take something like this:
APIshop.create([CreateCampaignService, {auth_token: User.token}, {item_id: Cart.item_id}]) and hit /shop/<item_id>?

Here is APIshop:

app.provider('APIshop', function(API_URL) {
    this.$get = ['$resource', function($resource) {
        var shop = $resource(API_URL.url + API_URL.loc + ':service/', {service: '@service'},   { 
            'create':  {method:'POST', isArray: false, params: {service: 'shop'}},
            'update':  {method:'PUT', isArray: false, params: {service: 'shop'}}
        });
        return shop;
    }];
});
theintellects
  • 1,320
  • 2
  • 16
  • 28

2 Answers2

0

You can't send it in the array since it is only accepting one param unless you concatenate the token and item id. That is bad practice. You should include the token in the header of the request.

take a look here for more info

Basic Authentication with a Guid token for REST api instead of username/password

Community
  • 1
  • 1
0

In https://docs.angularjs.org/api/ngResource/service/$resource under paramDefaults

Default values for url parameters. These can be overridden in actions methods. If any of the parameter value is a function, it will be executed every time when a param value needs to be obtained for a request (unless the param was overridden).

Each key value in the parameter object is first bound to url template if present and then any excess keys are appended to the url search query after the ?.

Given a template /path/:verb and parameter {verb:'greet', salutation:'Hello'} results in URL /path/greet?salutation=Hello.

If the parameter value is prefixed with @ then the value of that parameter will be taken from the corresponding key on the data object (useful for non-GET operations).

So this should work with TOKEN stored somewhere ($rootScope maybe):

app.provider('APIshop', function(API_URL) {
    this.$get = ['$resource', function($resource) {
        var shop = $resource(API_URL.url + API_URL.loc + ':service/:id', {service: '@service', id:'@id'},   { 
            'create':  {method:'POST', isArray: false, params: {service: 'shop', access_token: TOKEN}},
            'update':  {method:'PUT', isArray: false, params: {service: 'shop', access_token: TOKEN}}
        });
        return shop;
    }];
});
Issam Zoli
  • 2,724
  • 1
  • 21
  • 35