【问题标题】:How to make remote REST call inside Node.js? any CURL?如何在 Node.js 中进行远程 REST 调用?任何卷曲?
【发布时间】:2011-08-04 08:09:05
【问题描述】:

Node.js 中,除了使用子进程进行 CURL 调用之外,有没有办法对远程服务器进行 CURL 调用 REST API 并获取返回数据?

我还需要为远程 REST 调用设置请求标头,并且还需要在 GET(或 POST)中查询字符串。

我找到了这个:http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs

但它没有显示任何 POST 查询字符串的方法。

【问题讨论】:

标签: rest curl node.js


【解决方案1】:

http.request

var options = {
  host: url,
  port: 80,
  path: '/resource?id=foo&bar=baz',
  method: 'POST'
};

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

【讨论】:

  • 那么,即使是POST,我也要在查询字符串中追加数据?
  • @murvinlai 不确定。去阅读文档、源代码、HTTP 规范。不是该地区的专家。
  • 需要注意的一点是,您没有将 http 或 https 放在您的主机条目中,例如var options = { host: graph.facebook.com .... } 不是 {host: http: graph.facebook.com }。这让我绊倒了几个周期。 (见下文)。这两个都是很好的答案。谢谢你们。
  • 我能否指出,如果回复很长,使用 res.on('data',..) 是不够的。我相信正确的方法是让 res.on('end'..) 知道您何时收到所有数据。然后就可以处理了。
  • 这是一个非常古老的答案 - 对于今天编写节点 js 的人来说,您肯定会使用 npmjs.com/package/node-fetch 或其他基于 Fetch 标准的 fetch API 包。请参阅下面的答案。
【解决方案2】:

http://isolasoftware.it/2012/05/28/call-rest-api-with-node-js/

var https = require('https');

/**
 * HOW TO Make an HTTP Call - GET
 */
// options for GET
var optionsget = {
    host : 'graph.facebook.com', // here only the domain name
    // (no http/https !)
    port : 443,
    path : '/youscada', // the rest of the url with parameters if needed
    method : 'GET' // do GET
};

console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');

// do the GET request
var reqGet = https.request(optionsget, function(res) {
    console.log("statusCode: ", res.statusCode);
    // uncomment it for header details
    // console.log("headers: ", res.headers);

    res.on('data', function(d) {
        console.info('GET result:\n');
        process.stdout.write(d);
        console.info('\n\nCall completed');
    });

});

reqGet.end();
reqGet.on('error', function(e) {
    console.error(e);
});

/**
 * HOW TO Make an HTTP Call - POST
 */
// do a POST request
// create the JSON object
jsonObject = JSON.stringify({
    "message" : "The web of things is approaching, let do some tests to be ready!",
    "name" : "Test message posted with node.js",
    "caption" : "Some tests with node.js",
    "link" : "http://www.youscada.com",
    "description" : "this is a description",
    "picture" : "http://youscada.com/wp-content/uploads/2012/05/logo2.png",
    "actions" : [ {
        "name" : "youSCADA",
        "link" : "http://www.youscada.com"
    } ]
});

// prepare the header
var postheaders = {
    'Content-Type' : 'application/json',
    'Content-Length' : Buffer.byteLength(jsonObject, 'utf8')
};

// the post options
var optionspost = {
    host : 'graph.facebook.com',
    port : 443,
    path : '/youscada/feed?access_token=your_api_key',
    method : 'POST',
    headers : postheaders
};

console.info('Options prepared:');
console.info(optionspost);
console.info('Do the POST call');

// do the POST call
var reqPost = https.request(optionspost, function(res) {
    console.log("statusCode: ", res.statusCode);
    // uncomment it for header details
//  console.log("headers: ", res.headers);

    res.on('data', function(d) {
        console.info('POST result:\n');
        process.stdout.write(d);
        console.info('\n\nPOST completed');
    });
});

// write the json data
reqPost.write(jsonObject);
reqPost.end();
reqPost.on('error', function(e) {
    console.error(e);
});

/**
 * Get Message - GET
 */
// options for GET
var optionsgetmsg = {
    host : 'graph.facebook.com', // here only the domain name
    // (no http/https !)
    port : 443,
    path : '/youscada/feed?access_token=you_api_key', // the rest of the url with parameters if needed
    method : 'GET' // do GET
};

console.info('Options prepared:');
console.info(optionsgetmsg);
console.info('Do the GET call');

// do the GET request
var reqGet = https.request(optionsgetmsg, function(res) {
    console.log("statusCode: ", res.statusCode);
    // uncomment it for header details
//  console.log("headers: ", res.headers);


    res.on('data', function(d) {
        console.info('GET result after POST:\n');
        process.stdout.write(d);
        console.info('\n\nCall completed');
    });

});

reqGet.end();
reqGet.on('error', function(e) {
    console.error(e);
});

【讨论】:

  • 我如何访问来自 d 的值??? d = {"data":[{"id":1111,"name":"peter"}]} 。如何获取名称值?
  • 设法通过使用 var thed = JSON.parse(d); 获取值console.log("id 为:"+thed.data[0].id);但有时我得到“输入意外结束”
【解决方案3】:

我一直在使用restler 进行网络服务调用,效果很好,而且非常整洁。

【讨论】:

    【解决方案4】:

    您可以使用curlrequest 轻松设置您想要执行的请求时间......您甚至可以在选项中设置标题以“”浏览器调用。

    【讨论】:

      【解决方案5】:

      Request — Simplified HTTP client怎么样。

      2020 年 2 月编辑:请求已被弃用,因此您可能不应再使用它。

      这是一个 GET:

      var request = require('request');
      request('http://www.google.com', function (error, response, body) {
          if (!error && response.statusCode === 200) {
              console.log(body) // Print the google web page.
           }
      })
      

      OP 也想要一个 POST:

      request.post('http://service.com/upload', {form:{key:'value'}})
      

      【讨论】:

      • 在 google.com 上运行良好,但在请求 facebook 的图形 api 时返回“RequestError: Error: socket hang up”。请指导,谢谢!
      • 这个模块有很多问题!
      • 以这种方式使用 REST API 时如何传递请求参数?
      • 自 2020 年 2 月 11 日起,请求已完全弃用。你可以在网站上看到它github.com/request/request#deprecated
      • 对新手应该使用什么有任何指导吗?我正在通过大量使用它的示例进行过滤。
      【解决方案6】:

      警告:自 2020 年 2 月 11 日起,request 已完全弃用。

      另一个例子 - 你需要为此安装请求模块

      var request = require('request');
      function get_trustyou(trust_you_id, callback) {
          var options = {
              uri : 'https://api.trustyou.com/hotels/'+trust_you_id+'/seal.json',
              method : 'GET'
          }; 
          var res = '';
          request(options, function (error, response, body) {
              if (!error && response.statusCode == 200) {
                  res = body;
              }
              else {
                  res = 'Not Found';
              }
              callback(res);
          });
      }
      
      get_trustyou("674fa44c-1fbd-4275-aa72-a20f262372cd", function(resp){
          console.log(resp);
      });
      

      【讨论】:

        【解决方案7】:
        const http = require('http');
        const url = process.argv[2];
        
        http.get(url, function(response) {
          let finalData = "";
        
          response.on("data", function (data) {
            finalData += data.toString();
          });
        
          response.on("end", function() {
            console.log(finalData.length);
            console.log(finalData.toString());
          });
        
        });
        

        【讨论】:

          【解决方案8】:

          我没有找到任何带有 cURL 的东西,所以我写了一个围绕 node-libcurl 的包装器,可以在 https://www.npmjs.com/package/vps-rest-client 找到。

          做一个POST是这样的:

          var host = 'https://api.budgetvm.com/v2/dns/record';
          var key = 'some___key';
          var domain_id = 'some___id';
          
          var rest = require('vps-rest-client');
          var client = rest.createClient(key, {
            verbose: false
          });
          
          var post = {
            domain: domain_id,
            record: 'test.example.net',
            type: 'A',
            content: '111.111.111.111'
          };
          
          client.post(host, post).then(function(resp) {
            console.info(resp);
          
            if (resp.success === true) {
              // some action
            }
            client.close();
          }).catch((err) => console.info(err));
          

          【讨论】:

            【解决方案9】:

            如果您有 Node.js 4.4+,请查看reqclient,它允许您以 cURL 样式进行调用和记录请求,因此您可以轻松检查和重现调用在应用程序之外。

            返回Promise 对象而不是传递简单的回调,因此您可以以更“时尚” 的方式处理结果,chain 轻松处理结果,并以标准方式处理错误。还删除了每个请求的大量样板配置:基本 URL、超时、内容类型格式、默认标头、URL 中的参数和查询绑定,以及基本缓存功能。

            这是一个如何使用 curl 样式初始化、调用和记录操作的示例:

            var RequestClient = require("reqclient").RequestClient;
            var client = new RequestClient({
                baseUrl:"http://baseurl.com/api/", debugRequest:true, debugResponse:true});
            client.post("client/orders", {"client": 1234, "ref_id": "A987"},{"x-token": "AFF01XX"});
            

            这将登录控制台...

            [Requesting client/orders]-> -X POST http://baseurl.com/api/client/orders -d '{"client": 1234, "ref_id": "A987"}' -H '{"x-token": "AFF01XX"}' -H Content-Type:application/json
            

            当返回响应时...

            [Response   client/orders]<- Status 200 - {"orderId": 1320934}
            

            这是一个如何使用 promise 对象处理响应的示例:

            client.get("reports/clients")
              .then(function(response) {
                // Do something with the result
              }).catch(console.error);  // In case of error ...
            

            当然可以安装:npm install reqclient

            【讨论】:

              【解决方案10】:

              我使用node-fetch,因为它使用熟悉的(如果您是网络开发人员)fetch() API。 fetch() 是从浏览器发出任意 HTTP 请求的新方法。

              是的,我知道这是一个 node js 问题,但是我们不是要减少 API 开发人员必须记住和理解的数量,并提高我们 javascript 代码的可重用性吗? Fetch is a standard 那么我们如何收敛呢?

              fetch() 的另一个好处是它返回一个 javascript Promise,所以你可以像这样编写异步代码:

              let fetch = require('node-fetch');
              
              fetch('http://localhost', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: '{}'
              }).then(response => {
                return response.json();
              }).catch(err => {console.log(err);});
              

              获取替代种子XMLHTTPRequest。这是一些more info

              【讨论】:

              • 在编写 API 时 node-fetch 的问题是,仅适用于完整 URL,不适用于相对 URL。
              • 注意:fetch() 从 v17.5 开始进入核心 NodeJS 运行时 fusebit.io/blog/node-fetch
              【解决方案11】:

              Axios

              在 Node.js 中使用 Axios 的示例(axios_example.js):

              const axios = require('axios');
              const express = require('express');
              const app = express();
              const port = process.env.PORT || 5000;
              
              app.get('/search', function(req, res) {
                  let query = req.query.queryStr;
                  let url = `https://your.service.org?query=${query}`;
              
                  axios({
                      method:'get',
                      url,
                      auth: {
                          username: 'the_username',
                          password: 'the_password'
                      }
                  })
                  .then(function (response) {
                      res.send(JSON.stringify(response.data));
                  })
                  .catch(function (error) {
                      console.log(error);
                  });
              });
              
              var server = app.listen(port);
              

              确保在您的项目目录中执行以下操作:

              npm init
              npm install express
              npm install axios
              node axios_example.js
              

              然后您可以使用浏览器测试 Node.js REST API:http://localhost:5000/search?queryStr=xxxxxxxxx

              同样可以发帖,比如:

              axios({
                method: 'post',
                url: 'https://your.service.org/user/12345',
                data: {
                  firstName: 'Fred',
                  lastName: 'Flintstone'
                }
              });
              

              超级代理

              同样你可以使用 SuperAgent。

              superagent.get('https://your.service.org?query=xxxx')
              .end((err, response) => {
                  if (err) { return console.log(err); }
                  res.send(JSON.stringify(response.body));
              });
              

              如果您想进行基本身份验证:

              superagent.get('https://your.service.org?query=xxxx')
              .auth('the_username', 'the_password')
              .end((err, response) => {
                  if (err) { return console.log(err); }
                  res.send(JSON.stringify(response.body));
              });
              

              参考:

              【讨论】:

                【解决方案12】:

                警告:自 2020 年 2 月 11 日起,request 已完全弃用。

                如果您使用表单数据实现,请了解更多信息 (https://tanaikech.github.io/2017/07/27/multipart-post-request-using-node.js):

                var fs = require('fs');
                var request = require('request');
                request.post({
                  url: 'https://slack.com/api/files.upload',
                  formData: {
                    file: fs.createReadStream('sample.zip'),
                    token: '### access token ###',
                    filetype: 'zip',
                    filename: 'samplefilename',
                    channels: 'sample',
                    title: 'sampletitle',
                  },
                }, function (error, response, body) {
                  console.log(body);
                });
                

                【讨论】:

                  【解决方案13】:

                  使用最新的 Async/Await 功能

                  https://www.npmjs.com/package/request-promise-native

                  npm install --save request
                  npm install --save request-promise-native
                  

                  //代码

                  async function getData (){
                      try{
                            var rp = require ('request-promise-native');
                            var options = {
                            uri:'https://reqres.in/api/users/2',
                            json:true
                          };
                  
                          var response = await rp(options);
                          return response;
                      }catch(error){
                          throw error;
                      }        
                  }
                  
                  try{
                      console.log(getData());
                  }catch(error){
                      console.log(error);
                  }
                  

                  【讨论】:

                    【解决方案14】:

                    我发现 superagent 真的很有用, 这很简单 例如

                    const superagent=require('superagent')
                    superagent
                    .get('google.com')
                    .set('Authorization','Authorization object')
                    .set('Accept','application/json')
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2023-03-07
                      • 1970-01-01
                      • 2016-06-15
                      • 1970-01-01
                      • 1970-01-01
                      • 2013-05-20
                      • 1970-01-01
                      相关资源
                      最近更新 更多