1

I need to GET from a url of this pattern api/v1/album/:albumId/song/:songId/

How do I pass these params in the url.

I tried using params in
params: { albumId: 2, songId: 4 }

but the GET call was made to api/v1/album/:albumId/song/:songId/?albumId=2&songId=4 instead of api/v1/album/2/song/4/

How can this be done without using string concatenation?

2
  • Consider using $resource instead of $http. $resource knows how to interpolate your URL pattern but $http does not. Commented Apr 18, 2016 at 9:16
  • Yup, I started working in that direction. Thanks Commented Apr 18, 2016 at 9:28

2 Answers 2

1

If you use RESTful service, $resource can make this work easier:

More info here: https://docs.angularjs.org/api/ngResource/service/$resource

Example code:

var Song = $resource('/api/v1/album/:albumId/song/:songId/',
                      {albumId: '@albumId', songId: '@songId'});

Song.get({albumId: 2, songId: 4}, function(song) {
    //returned song object

});

//or chaining with promise object
Song.get({albumId: 2, songId: 4})
    .$promise.then(function(song){
    //success handler
    $scope.selectedSong = song;
}, function() {
    //failure handler
});

There are more options depends on your requirement. Hope this can help you :)

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

Comments

0

Params: { .. } are concatenated on the url as querystring which happened in your case. Simply construct your url having the parameter values included e.g.

var url = 'api/v1/album/1/song/2';
$http.get(url).then(function(){ .... });

2 Comments

Since the values of albumId and songId depend on the user's input, would using string concatenation be a secure option? Is there any way apart from using string concatenation?
There is no difference between the Params approach vs the one i've posted it is still getting the input from the user, if you want it to be secured then validate first the ids before calling get.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.