如何在node.js中进行HTTP POST请求?(How is an HTTP POST request made in node.js?)

编程入门 行业动态 更新时间:2024-10-24 14:23:07
如何在node.js中进行HTTP POST请求?(How is an HTTP POST request made in node.js?)

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

How can I make an outbound HTTP POST request, with data, in node.js?

最满意答案

以下是使用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命令来实现这一点,在成功读取后发布实际的代码。 如果出现错误,则会抛出该错误,如果没有数据,则该进程退出并显示负值以指示失败。

Here's an example of using node.js to make a POST request to the Google Compiler API:

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

I've updated the code to show how to post data from a file, instead of the hardcoded string. It uses the async fs.readFile command to achieve this, posting the actual code after a successful read. If there's an error, it is thrown, and if there's no data the process exits with a negative value to indicate failure.

更多推荐

本文发布于:2023-07-24 20:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   node   js   request   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!