我需要进行 XML POST(我知道不要问政府...),但我无法让它与原生 Node 请求 promise 一起工作。
我尝试将我的 XML 字符串转换为缓冲区、String()、.toString 等。如果我将 json:true 转换为 POST,那么我认为这不是网络问题(当传递带有 json true 的 xml 字符串时,它会发送类似 { 'variablename': 'stringed XML that I要作为正文发送' } 的 json )
这是我正在处理的内容。我已经在这里碰头一段时间了,感谢任何帮助。
理想情况下是 promise /异步。
也许我应该寻找 XMLHttp 请求 npm 模块?
var request_promise_native_options = {
method: 'POST',
uri: 'http://requestbin.fullcontact.com/198flbs1',
headers: {
'User-Agent': 'Request-Promise',
'Content-Type': 'text/xml'
//'Content-Length': Buffer.byteLength(my_xml_string) //I've tried multiple ways to use this
},
body: {
my_xml_string //also tried many ways here Buffer, String() etc
},
json: false // automatically stringifys body to json if true
};
request_promise(request_promise_native_options)
.then(function (response) {
console.log("success");
})
.catch(function (err) {
console.log(err);
})
最佳答案
感谢 @kevin-b 帮助我看到显而易见的事情。只需删除 {}
var request_promise_native_options = {
method: 'POST',
uri: 'http://requestbin.fullcontact.com/198flbs1',
headers: {
'User-Agent': 'Request-Promise',
'Content-Type': 'text/xml'
'Content-Length': Buffer.byteLength(my_xml_string)
},
body: my_xml_string,
json: false // automatically stringifys body to json if true
};
关于node.js - 如何使用 Node 中 native 的请求 promise 来使用 XML 主体/内容类型 header 进行 POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51089563/