当前位置: 首页 > 面试题库 >

如何在node.js中发出HTTP POST请求?

乐欣可
2023-03-14
问题内容

如何在node.js中使用数据发出出站HTTP POST请求?


问题答案:

这是一个使用node.js向Google Compiler API发出POST请求的示例:

// We need this to build our post string
var querystring = require('querystring');
var http = require('http');
var fs = require('fs');

function PostCode(codestring) {
  // Build the post string from an object
  var post_data = querystring.stringify({
      'compilation_level' : 'ADVANCED_OPTIMIZATIONS',
      'output_format': 'json',
      'output_info': 'compiled_code',
        'warning_level' : 'QUIET',
        'js_code' : codestring
  });

  // An object of options to indicate where to post to
  var post_options = {
      host: 'closure-compiler.appspot.com',
      port: '80',
      path: '/compile',
      method: 'POST',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': Buffer.byteLength(post_data)
      }
  };

  // Set up the request
  var post_req = http.request(post_options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
          console.log('Response: ' + chunk);
      });
  });

  // post the data
  post_req.write(post_data);
  post_req.end();

}

// This is an async file read
fs.readFile('LinkedList.js', 'utf-8', function (err, data) {
  if (err) {
    // If this were just a small part of the application, you would
    // want to handle this differently, maybe throwing an exception
    // for the caller to handle. Since the file is absolutely essential
    // to the program's functionality, we're going to exit with a fatal
    // error instead.
    console.log("FATAL An error occurred trying to read in the file: " + err);
    process.exit(-2);
  }
  // Make sure there's data before we post it
  if(data) {
    PostCode(data);
  }
  else {
    console.log("No data to post");
    process.exit(-1);
  }
});

我已经更新了代码,以显示如何从文件而不是硬编码的字符串发布数据。它使用async
fs.readFile命令来实现此目的,并在成功读取后发布实际代码。如果有错误,则抛出该错误,如果没有数据,则该过程以负值退出以指示失败。



 类似资料:
  • 问题内容: 问题很简单。我想使用Node.js服务器作为代理,以记录,认证HTTP查询并将其转发到后端HTTP服务器(PUT,GET和DELETE请求)。 我应该为此使用哪个库?恐怕找不到。 问题答案: NodeJS支持将http.request作为标准模块:http ://nodejs.org/docs/v0.4.11/api/http.html#http.request

  • 问题内容: 我在iBooks中阅读了Apple的The Programming Language Swift ,但无法弄清楚如何在Swift中发出HTTP请求(例如cURL)。我需要导入Obj- C类还是只需要导入默认库?还是无法基于本机Swift代码发出HTTP请求? 问题答案: 您可以使用,而且还是因为你通常在Objective- C做。请注意,对于iOS 7.0和更高版本,它是首选。 使用

  • 我在iBooks上读过Apple的编程语言Swift,但不知道如何在Swift中发出HTTP请求(类似于cURL)。我需要导入Obj-C类还是只需要导入默认库?或者不可能基于本机Swift代码发出HTTP请求?

  • 我在iBooks中阅读了苹果公司的编程语言Swift,但不知道如何在Swift中发出HTTP请求(类似cURL的东西)。我需要导入Obj-C类还是只需要导入默认库?还是不能基于原生Swift代码进行HTTP请求?

  • 对传递的 URL 发出一个 POST 请求。 使用 XMLHttpRequest web api 对给定的url 发出一个 post 请求。 用 setRequestHeader 方法设置 HTTP 请求头的值。 通过调用给定的 callback 和 responseText 来处理 onload 事件。 通过运行提供的 err 函数,处理onerror事件。 省略第三个参数 data ,不发送数

  • 问题内容: 我正在尝试使用node.js将http请求发送到neo4j数据库。这是我正在使用的代码: 我检查数据库是否正在运行(我已连接到管理网页,并且一切正常。)恐怕问题不在数据库方面,而在node.js方面。 我希望有人可以对此问题有所启发。我想学习如何在node.js中发送http请求,答案不必特定于neo4j问题。 提前致谢 问题答案: 如果是简单的GET请求,则应使用 否则,需要关闭。