2

I'm using an AngularJS $resource factory to access a client REST services. Some of the parameters are defined with optional path parameters rather than query parameters.

Case 1:

I call myUrl/application-:applicationID/limit-:limit.json

with:

{
    applicationID:"1234",
    limit: 10
}

I get the correct URL of myUrl/application-1234/limit-10.json

Case 2:

I call myUrl/application-:applicationID/limit-:limit.json

with:

{
    applicationID:"1234"
}

I want the resulting url to ignore the optional limit parameter and collapse down to myUrl/application-1234.json

However, what I get is myUrl/application-1234/limit-.json

*EDIT I have several API categories eg Foo1, Foo2, and each has several endpoints eg endPoint1, endPoint2 and using the method in @Deiter's post, I can't see a way to cleanly list all my resources without using temporary category names then binding them all together at the end.

MyServices.factory ( 'MyAPI', ['$resource',

function ( $resource ) {

    return {

    Foo1: $resource ( '', 
    {                       
        application: "@applicationID",
        limit: "@limit"
    }, 
    {           
        endPoint1: {
            ...
        },
        endPoint2: { 
            url: "myURL/application-:applicationID/limit-:limit.json",
            method:'GET', 
            isArray:true
        },
    }),     

    Foo2: $resource ( '', 
    {                       
        application: "@applicationID"
    }, 
    {           
        endPoint1: { 
            ...
        },
        endPoint2: { 
            ...
        }
    }),             
}               
}]);    

Example call:

MyAPI.Foo1.endPoint1 ( { application: "1", limit: 5 } ); 
MyAPI.Foo2.endPoint2 ( { application: "1" } ); 
2
  • Possible duplicate of optional-url-parameters-in-angularjs-resource Commented Feb 6, 2014 at 13:49
  • I've edited my original post to show my current structure as I'm not sure how to cleanly integrate the method in your post. Commented Feb 6, 2014 at 14:54

1 Answer 1

1

You could use the related answer given in the comments by @Dieter, and create separate $resources, but I would suggest simply making each piece of the URL consistent. Do this by either:

1) Removing the static component of each variable part (eg. myUrl/1234/10)

2) Passing the actual value to the URL that you want to use, and keeping the entire part variable (eg. myUrl/:applicationID/:limit, and pass in application-1234 & limit-10)

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

3 Comments

I've edited my original post to show the current structure. If I understand you correctly, you suggest that I pass in the key and value pair in the string parameter rather than just the value?
Yeah. But I also feel like the application- and limit- are quite redundant.
Thanks. I agree with you about the url structure but there are quite a lot of endpoints and it's already been deployed on a large multi-platform site, so unfortunately it's very unlikely they will change it.

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.