Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

with this code I am trying to send a $http.get request to my rest service:

$http({
    method: 'GET',
    url: '/api/item/all',
    params: {
        query: {
            userid: 7
        },
        fields: 'title'
    }
});

I expected to access this URL:

/api/item/all?query[userid]=7&fields=title

but I am always getting a malformed url...

So, how to proceed?

Or even better: Is it possible to pass the full request url into the url-param of $http?

share|improve this question
    
You should be fine to write the url with the full query string unless there are variables –  Explosion Pills Jul 11 at 23:42
    
could try "query[userid]" : 7 , fields: 'title' –  charlietfl Jul 11 at 23:46
    
@ExplosionPills the user id is a variable... should I first create a string with the url and then pass it to $http? –  IgelHaut Jul 11 at 23:48
    
@charlietfl already tried... doesn't pass the variable at all. –  IgelHaut Jul 11 at 23:49
    
OK. I didn't realize it was a variable. Can you change back end, that's an ugly looking url especially since userid is a variable. Alternatively creating the string in advance is not an issue either –  charlietfl Jul 11 at 23:56

2 Answers 2

up vote 1 down vote accepted

In a GET request, you should pass parameters as part of the query string.

You can use the shortcut get call and pass in the query parameters in the url:

$http.get('api/item/all?userid=" + 7 + "&fields=title");

If you need to access them as scope variables, you can do that too:

$http.get('api/item/all?userid=" + $scope.foo + "&fields=" + $scope.bar);

Also depending on the back end you can send them as a path parameters rather then a query parameter:

$http.get('api/item/all/userid/" + $scope.foo + "/fields/" + $scope.bar);

Hope that helps

share|improve this answer

you can create get request in this way

 $http.get('/api/item/all?query[userid]=7&fileds=title');

or ...

$scope.userid = 7;
$scope.fields = "title"

$http.get('/api/item/all?query[userid]='+$scope.userid+'&fileds='+$scope.fields+');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.