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

I have tried a few different methods to get filtered data back from the REST API but cannot figure out the correct formatting for queries. This work for me:

var query = encodeURIComponent('where=' + '{"' + type + '":"' + subtype + '"}');

      $http.get('https://api.parse.com/1/classes/events?' + query,
             {headers:{
                'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                'Content-Type' : 'application/json'
            }
          })
    },

But when trying to include the additional parameters such as 'include', 'limit', and 'count' I have tried many iterations, but to no avail. Most queires returned a bad response, however this query returns an empty array (which isn't what I would like, but at least it returned something):

var query = encodeURIComponent('where=' + '{"' + type + '":"' + subtype + '", "limit":2}')

I have also tried using a params object with Angular's $http object but even with the same formatting as the above query, it returns a bad response. Can anyone give me the correct formatting for this as well? This, for example, does not seem to work:

   $http.get('https://api.parse.com/1/classes/events',
                     {params: {"where" : encodeURIComponent('{"senses":"touch"}') }},
                     {headers:{
                        'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                        'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                        'Content-Type' : 'application/json'
                    }
                  })

Thanks!

share|improve this question
    
Why aren't using Parse.com Javascript API? –  New Dev Oct 22 '14 at 3:47
    
Attempting to be platform agnostic - going with the Javascript API would be a last-resort I suppose. –  John Mastri Oct 22 '14 at 3:49
    
You should create a service to abstract away all these $http calls from the controller anyway. That will also make it "platform agnostic". –  New Dev Oct 22 '14 at 3:52

1 Answer 1

up vote 1 down vote accepted

You are "overthinking" this since a lot of the problems with encoding are quite common and have the benefit of being already solved. So, you don't need to do string formatting/encoding with $http - just use objects.

Also, after checking parse.com API, limit, count and include are siblings of where - not children.

So, all you need to do is:

var whereQuery = {type: subtype};

$http.get('https://api.parse.com/1/classes/events',
   {
     headers: {
                'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                'X-Parse-REST-API-Key': PARSE_CREDENTIALS.REST_API_KEY,
                'Content-Type' : 'application/json'
              },
     params:  { 
                 where: whereQuery,
                 limit: 2,
                 // count: 1
                 // include: "something"
              }
   });
share|improve this answer
    
Thanks for the answer - much appreciated - unfortunately it's still not working for me. return $http.get('https://api.parse.com/1/classes/events', { headers:{ 'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID, 'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY, 'Content-Type' : 'application/json' }, params: {where : {senses:"touch", limit:2} } }) returns an empty array. If I remove limit:2 from the object, it works and returns all objects. Any ideas? –  John Mastri Oct 24 '14 at 21:22
    
I think limit shouldn't be part of where... but that is beyond the scope of your question and my answer –  New Dev Oct 24 '14 at 21:32
    
I agree it doesn't make sense to put it inside of the where statement - I'm trying to figure out how to format it so that you can have a where statement with other parameters. Is it to use an ampersand, multiple objects, etc..? –  John Mastri Oct 25 '14 at 16:07
    
@JohnMastri, I updated the answer. Hopefully it's enough to understand the principles of how this works, and that with $http you don't need to do the kind of formatting/encoding that you tried in your question –  New Dev Oct 25 '14 at 23:00
    
Thanks - was definitely making it hard than it needed to be. =) –  John Mastri Oct 29 '14 at 1:46

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.