title: node.js Https请求传递数据
date: 2019-04-24 15:23:00
tags: [“https”, “node.js”]
我的代码如下:
参考了网上大多说的是req.write(contents);我试了这样写没有效果
而是在url拼接?contents 来传递数据
import * as https from "https";
var querystring = require('querystring');
//传递到后台的数据
var data = "1";
var post_data = { classId: data };
var contents = querystring.stringify(post_data);
var options = {
hostname: "www.baidu.com",
port: 443,
path: "/bfi-web/api/student/course/list_new?" + contents,
method: "POST",
rejectUnauthorized: false,
headers: {
Accept: "*/*",
"Accept-Encoding": "utf-8",
"Accept-Language": "zh-CN,zh;q=0.8",
Connection: "keep-alive",
Host: "www.eluzhu.com",
token: token,
version: "2.6",
from: "IDE",
},
};
var mData = '';
var req = https.request(options, function (res) {
res.setEncoding('utf-8');
res.on("data", function (d) {
console.log('===================================data===========' + d);
mData += d;
});
res.on("end", function () {
var data = JSON.parse(mData);
// console.log('===================================data===========' + mData);
if (data.code == "000000") {
MyTreeProvider.datas = data.data.list;
MyTreeProvider.initMyTreeList();
}
else {
vscode.window.showInformationMessage("请求失败:" + data.msg);
}
});
});
// req.write(contents);
req.on("error", function (e) {
console.error("====================================================" + e);
});
req.end();
本文参考来自:https://www.imooc.com/qadetail/181878