这是我的代码。如何将表单数据发送到外部服务器?谁能解决我的麻烦? 提前谢谢。
var http = require('http');
var FormData = require('form-data');
var form = new FormData();
form.append('books', ['book1', 'book2']); // my form data
var options = {
host: 'http://localhost',
port: 8080,
path: '/app/updateBooks',
method: 'POST'
};
http.request(options, function(resp){
console.log(resp);
}).on("error", function(err){
console.log(err);
});
答案 0 :(得分:1)
请求模块可用于向外部服务器发送数据: https://www.npmjs.com/package/request
例如:
const request = require('request')
request({
uri: 'https://www.example.com/',
qs: { access_token: token }, // object containing querystring values to be appended to the uri
method: 'POST',
json: messageData //json - sets body to JSON representation of value and adds Content-type: application/json header
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
//success condition
} else {
// failure condition
}
})