如何从NodeJS中的curl命令获得完整的有效负载?

时间:2015-08-22 12:12:24

标签: javascript json node.js curl

下面的javascript块在NodeJS框上运行,其目标是执行curl请求并输出返回的有效负载。

问题是,如果请求中返回的有效负载变大,则不会输出完整的有效负载。

如何让退回的stdout等待'直到它收到完整的JSON有效载荷?

stdout返回的小JSON有效负载示例(这很好):

{
    "Small data set": "Blablabla",
    ...etc...
    "End of data set": "Blablabla"
}

stdout返回的大JSON有效负载示例(这出错了):

{
    "Big data set": "Blablabla",
    ...etc...
    "End of 

注意:请参阅代码中的<-- PROBLEM IS HERE条评论。

function curlReq(a, i, callback){
  curl_request = {
      "url": "URL",
      "headers": [
          "HEADERS"
      ],
      "data": {
          "DATA"
    }
  };

  /**
   * Merge together in curl request (curl -i -H --data [url])
   */
  curl_render = 'curl -H "'+curl_request.headers[0]+'" '+
      ' -H "'+curl_request.headers[1]+'" '+
      ' -H "'+curl_request.headers[2]+'" '+
      " --data '"+JSON.stringify(curl_request.data)+"' "+
      curl_request.url;

  exec(curl_render, function (error, stdout, stderr) {
      callback(stdout); // <-- PROBEM IS HERE
  });
}

http.createServer(function (req, res) {
  if (req.method === 'GET') {
    /**
     * GET a = action
     * GET v = value
     */
    q = url.parse(req.url, true).query;
    if(q.a && q.i){
      curlReq(q.a, q.i, function(stdout){
         res.writeHead(200, {"Content-Type": "application/json"});
         res.end(stdout); // <-- PROBEM IS ALSO HERE
      });
    } else {
      /**
       * Nothing found to process so end it.
       */
      res.end('No action available');
    }
  }
}).listen(80);

0 个答案:

没有答案