gpt4 book ai didi

node.js - 如何在 node.js http.request 中发布 XML 数据

转载 作者:IT老高 更新时间:2023-10-28 22:59:31 25 4
gpt4 key购买 nike

我正在尝试使用 http.request 通过 Node.js 向 Web 服务提交 xml 请求。

这是我的代码。我的问题是,我想将 xml 发布到服务而不是 data=1

http.request({
host: 'service.x.yyy.x',
port: 80,
path: "/a.asmx?data=1",
method: 'POST'
}, function(resp) {
console.log(resp.statusCode);
if(resp.statusCode) {
resp.on('data', function (chunk) {
console.log(chunk);
str += chunk;
});
resp.on('end', function (chunk) {
console.log(str);
});
}
}).end();

怎么做?

最佳答案

其实Andrey Sidorov给出的链接帮助让它工作。这行得通。

var body = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'+
'<soap12:Body>......</soap12:Body></soap12:Envelope>';

var postRequest = {
host: "service.x.yyy.xa.asmx",
path: "/a.asmx",
port: 80,
method: "POST",
headers: {
'Cookie': "cookie",
'Content-Type': 'text/xml',
'Content-Length': Buffer.byteLength(body)
}
};

var buffer = "";

var req = http.request( postRequest, function( res ) {

console.log( res.statusCode );
var buffer = "";
res.on( "data", function( data ) { buffer = buffer + data; } );
res.on( "end", function( data ) { console.log( buffer ); } );

});

req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

req.write( body );
req.end();

关于node.js - 如何在 node.js http.request 中发布 XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14018269/

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