app.get('/curl', function(request, response) {
var data = querystring.stringify({
name : "bob"
});
var options = {
host: 'localhost',
port: 80,
path: 'http://matricore.com/curl_data/index.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
var req = http.request(options, function(response) {
console.log('STATUS: ' + response.statusCode);
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write('data\n');
req.write('data\n');
response.send(data);
req.end();
});
我一直在尝试将一些静态数据发布到某些网址,但它不起作用,需要帮助。
您的 http 请求有问题。选项中的 Host
应该是域名,path
是端点到域名的相对路径。
app.get('/curl', function(request, response) {
var data = querystring.stringify({
name : "bob"
});
var options = {
host: 'matricore.com',
port: 80,
path: '/curl_data/index.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
var req = http.request(options, function(response) {
console.log('STATUS: ' + response.statusCode);
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write('data\n');
req.write('data\n');
response.send(data);
req.end();
});
更新3
这肯定有效
var request = require('request');
app.get('/curl', function (req, res) {
request({
method: 'POST',
url:'http://matricore.com/curl_data/index.php',
form: {name:'bob'}
},function(err,httpResponse,body){
res.send(body);
});
});
我是一名优秀的程序员,十分优秀!