微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

node作为中间服务层如何发送请求(发送请求的实现方法详解)

GET请求:

rush:xhtml;"> var http = require('http'); var qs = require('querystring'); var data = { a: 123,time: new Date().getTime()};//这是需要提交的数据 var content = qs.stringify(data); var options = { hostname: '127.0.0.1',port: 10086,path: '/pay/pay_callback?' + content,method: 'GET' };

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

req.end();

POST请求:

rush:xhtml;"> var http = require('http'); var qs = require('querystring'); var post_data = { a: 123,time: new Date().getTime()};//这是需要提交的数据 var content = qs.stringify(post_data); var options = { hostname: '127.0.0.1',path: '/pay/pay_callback',method: 'POST',headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } };

var req = http.request(options,function (e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(content);
req.end();

以上这篇node作为中间服务层如何发送请求(发送请求的实现方法详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

原文地址:https://www.jb51.cc/nodejs/34389.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐