2012-07-27 3 views
1

안녕하세요, Node.js의이 Post 요청에 쿼리를 추가하려고합니다. 나는 그렇게하는 방법을 모르겠다. 여기 내가 사용하고 POST 요청 코드,Node.js의 Post 요청에 쿼리를 추가하는 방법

var options = { 
    host: 'ws.ispeech.org', 
    port: 80, 
    path: '/api/rest/1.5', 
    method: 'POST', 
}; 

var http = require('http'); 
var req = http.request(options, function(res) { 
    console.log('STATUS: ' + res.statusCode); 
    console.log('HEADERS: ' + JSON.stringify(res.headers)); 
    res.setEncoding('utf8'); 
    res.on('data', function (chunk) { 
    console.log('BODY: ' + chunk); 
    }); 
}); 

req.on('error', function(e) { 
    console.log('problem with request: ' + e.message); 
}); 

// write data to request body 
req.write('data\n'); 
req.write('data\n'); 
req.end(); 

답변

3

query string는 Node.js를 옵션 객체는 "경로"를 호출하는 URL의 일부입니다. 또한 당신을 위해 propertly/값 개체를 인코딩하는 "querystring" module이 있음을

var query = 'foo=bar&abc=123'; 
var options = { 
    host: 'ws.ispeech.org', 
    port: 80, 
    path: '/api/rest/1.5' + '?' + query, 
    method: 'POST', 
}; 

주 : 그래서 당신은 단순히 경로에 쿼리 문자열을 추가 할 수 있습니다

var query = querystring.stringify({foo:'bar', abc:123}); 
// query => "foo=bar&abc=123" 
관련 문제