我希望从我的 NodeJS 服务器执行 POST 请求。它需要发送原始 xml 数据。
建议采用什么方法?
编辑:添加我当前的沙箱。尚未成功!
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser());
var data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://api.__DOMAIN__.com"><soapenv:Header><api:userToken><password>pwd1</password><username>usr_api</username></api:userToken></soapenv:Header><soapenv:Body><api:SearchModels><!--Optional:--><searchText>keyword</searchText></api:SearchModels></soapenv:Body></soapenv:Envelope>';
var options = {
host: 'www.__MY_END_POINT_.com', port:443,
method: 'POST',
headers: {
'Content-Type': 'text/xml',
'Content-Length': data.length
}
};
var req = http.request(options, function(res)
{
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log("body: " + chunk);
});
});
req.write(data);
req.end();
谢谢, J.
最佳答案
好吧,我对所需模块的理解是错误的。
错误: var http = require('http');
好方法: var https = require('https');
关于xml - Node.js:使用 "raw"格式执行 POST 请求的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26467075/