2013-01-20 3 views
4

나는 아래 parse.com 휴식 API의 예를 복제하는 것을 시도하고 데이터 인코딩을 사용하는 방법 : 그래서사용 Parse.com REST API를 - CURL

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 

을 기반으로 내가 그렇게 전화

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(); 
}; 

:

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); 
    }); 

내 질문에 유래에서 발견 예에, 나는 기능을 구현 "{-"playerName ":"Sean Plott ","cheatMode ": false} '비트를"- data-urlencode "로 보내려면 어떻게해야합니까? 나는 '/ 1/classes/GameScore? playerName = John'과 같은 옵션에 경로를 설정하여 경로에 추가하려고 시도했지만 작동하지 않았다. John의 게임 스코어가 아닌 GameScore를 모두 받았다.

+0

왜 그냥 공식 구문 분석 자바 스크립트 SDK를 사용하지 않는? 또한 소스 코드를보고 나머지 API를 사용하는 방법을 볼 수 있습니다. https://npmjs.org/package/parse – bklimt

답변

10

나는 그런 옵션에 경로를 설정하여 경로에 추가 시도 : /1/classes/GameScore?playerName=John

그것은 값으로 키/이름으로 전체 JSON 값 where을 기대하고있는 것 같다

:

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

너는 이것을 얻을 수있다. querystring.stringify() :

선택적으로 JSON.stringify()
var qs = require('querystring'); 

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

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

// ... 

객체에서 값을 포맷하기 :

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

매력처럼 작동합니다! 고마워요! –

관련 문제