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.

In my RESTful api one of the resources exposes a GET method that accept json as a parameter named 'query'. This parameter is passed directly to the MongoDB query allowing users to query the database directly using mongo syntax.

The problem I'm having is that the request always looks like this:

?&query=%7B%22source%22:%22incident%22%7D 

Where it should look something like this:

?&query={'source': 'incident'} 

This is how I am sending the GET request:

var query = {};
if ($scope.sourceFilter) { query.source = $scope.sourceFilter; }
    var query = JSON.stringify(query);
    $http.get('/api/feedbackEntries', {params: {limit: $scope.limit, query: query}}).success(function(data) { .......

I am doing the same thing on other get requests and I don't get this issue.

Am I doing something wrong here ? Is this to do with the way angular parses params ?

Thanks

share|improve this question

3 Answers 3

Like the $http docs say

params{Object.<string|Object>} – Map of strings or objects which will be turned to ?key1=value1&key2=value2 after the url. If the value is not a string, it will be JSONified.

Latter emphasis is added by me.

So the query property of the object you pass to the params configuration option is an Object. This means is will be JSONified, which means the same as

JSON.stringify(query);

So this

{'source': 'incident'}

Turns to this:

'{"source": "incident"}'

As RFC 1738 states:

... only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.

As it happens {, } and " are not on that list and have to be url encoded to be used in a url. In your case %7B corresponds to {, %7D corresponds to } and %22 corresponds to ".

So what is happening is normal and most server software automatically decodes the url query parameters for you, so they will be presented normally. Most likely you'll need to parse it back to JSON somehow!

Hope this helps!

share|improve this answer
    
Hi Jake. Are you saying I shouldn't be adding parameters to GET requests ? I can't see how this goes against what HTTP GET requests are meant for .. maybe I have misunderstood. I don't think I should use POST here as I am not creating anything, I'm simply GETting data from the database based on the query provided. –  Sherlock Feb 13 at 11:59
    
Nope, i misread your question, I'll edit to answer your question –  jakee Feb 13 at 12:00
    
There you go, voila! –  jakee Feb 13 at 12:09
    
Cheers Jake. This is helpful. Still a bit confused about why this is only happening on this request. I'll keep digging though. Thanks. –  Sherlock Feb 13 at 12:20

When using RESTFul apis concider using ngResource, ngResource docs

Include it in your module:

yourApp = angular.module('yourApp', ['ngResource'])

Add your service:

yourApp.factory('YourService', ['$resource', function($resource){
return $resource('link/to/your/object', {});
}]);

Add your controller

yourApp.controller('YourController', [$scope, 
'YourService', function($scope, YourService) {

$scope.yourData = YourService.get(); 
$scope.yourData = YourService.query(); //(When obtaining arrays from JSON.)

I've found this is the best way using a RESTFull api.

share|improve this answer
    
unfortunately for reasons that are out of my control i'm unable to use ngResource and I am stuck with $http for this project. –  Sherlock Feb 13 at 12:00
up vote 0 down vote accepted

After some digging I figured this one out. Looking at the request I was making:

$http.get('/api/feedbackEntries',

I saw that the url does not end with a trailing slash. This was the only difference I could see compared with other requests that were working fine. So I added the trailing slash and magically it works. I can't explain why, whether it's something within angular or elsewhere .. but this is how I fixed the problem.

Hope this helps someone in the future.

share|improve this answer
    
What bothers me here is that if you would have used ngResource, that would have taken care of your trailing slashes! So my votedown is pretty much very evil. –  petur Feb 20 at 12:22
    
@petur - I agree, ngResource is preferable here, and I wish I could use it. However, it was not me who downvoted! But, because i'm such a great person, I upvoted your answer so that you don't lose any rep ;D –  Sherlock Feb 20 at 13:06
    
Oh, I'm terribly sorry that I assumed that, and thanks for the up vote. –  petur Feb 20 at 13:54

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.