nodejs如何向别的网站发送post请求,并且带有参数
nodejs如何向别的网站发送post请求,并且带有参数
20 回复
const rp = require('request-promise')
const options = {
url, headers,......
}
rp.post(options).then(t => {
......
})
这个看node文档就能搞定啊。。。 贴一段用http原生模块写的,不喜欢promise的化,把promise扔掉好了
var http = require('http');
var Promise = require("bluebird");
module.exports = function(options, body) {
return new Promise(function(resolve, reject) {
var httpBody = options.body || body;
var req = http.request(options, (res) => {
var chunks = [];
res.on('data', (chunk) => {
chunks.push(chunk);
});
res.on('end', () => {
resolve({
statusCode: res.statusCode,
headers: res.headers,
body: Buffer.concat(chunks)
});
})
});
if (httpBody) {
req.write(httpBody);
}
req.end();
req.on('error', (e) => {
reject(e);
})
if (options.timeout) {
req.setTimeout(options.timeout, () => {
req.abort();
})
}
})
}