gpt4 book ai didi

javascript - 如何将表单数据从 Node 服务器传递到外部服务器?

转载 作者:行者123 更新时间:2023-12-03 03:55:38 25 4
gpt4 key购买 nike

这是我的代码。如何将我的表单数据发送到外部服务器?有人能解决我的烦恼吗?提前致谢。

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

最佳答案

请求模块可用于向外部服务器发送数据: 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
}
})

关于javascript - 如何将表单数据从 Node 服务器传递到外部服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44965679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com