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.

I am trying to replicate the example of the parse.com rest API below:

curl -X GET \
  -H "X-Parse-Application-Id: APP_ID" \
  -H "X-Parse-REST-API-Key: API_KEY" \
  -G \
  --data-urlencode 'where={"playerName":"John"}' \
  https://api.parse.com/1/classes/GameScore

So, based on an example found on Stackoverflow, I implemented the function:

var https = require("https");
exports.getJSON = function(options, onResult){

    var prot = options.port == 443 ? https : http;
    var req = prot.request(options, function(res){
        var output = '';
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            output += chunk;
        });

        res.on('end', function() {
            var obj = JSON.parse(output);
            onResult(res.statusCode, obj);
        });
    });

    req.on('error', function(err) {
    });

    req.end();
};

which I call like that :

var options = {
host: 'api.parse.com',
port: 443,
path: '/1/classes/GameScore',
method: 'GET',
headers: {
    'X-Parse-Application-Id': 'APP_ID',
    'X-Parse-REST-API-Key': 'APP_KEY'
}
};

rest.getJSON(options,
    function(statusCode, result)
    {
        // I could work with the result html/json here.  I could also just return it
        //console.log("onResult: (" + statusCode + ")" + JSON.stringify(result));
        res.statusCode = statusCode;
        res.send(result);
    });

My question is, how do I send the "--data-urlencode 'where={"playerName":"Sean Plott","cheatMode":false}' bit? I tried appending it to the path by setting the path in the options like that: '/1/classes/GameScore?playerName=John, but that didn't work, I received all the GameScore, not the ones from John

share|improve this question
    
Why don't you just use the official Parse JavaScript SDK? Also, you can look at its source code to see how to use the rest api. npmjs.org/package/parse –  bklimt Jan 20 '13 at 9:16

1 Answer 1

up vote 4 down vote accepted

I tried appending it to the path by setting the path in the options like that: /1/classes/GameScore?playerName=John

It seems to be expecting where as the key/name with the value the entire JSON value:

/1/classes/GameScore?where=%7B%22playerName%22%3A%22John%22%7D

You can get this with querystring.stringify():

var qs = require('querystring');

var query = qs.stringify({
    where: '{"playerName":"John"}'
});

var options = {
    // ...
    path: '/1/classes/GameScore?' + query,
    // ...
};

// ...

Optionally with JSON.stringify() to format the value from object:

var query = qs.stringify({
    where: JSON.stringify({
        playerName: 'John'
    })
});
share|improve this answer
    
Works like a charm!!! Thanks a lot! –  Cyril Gaillard Jan 20 '13 at 7:21

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.