如何使用 Node 中的本机请求承诺制作带有 XML 正文/内容类型标头的 POST?

Dil*_*yle 0 xml request node.js xml-parsing request-promise

我需要制作一个 XML POST(我知道不要问它是政府......)而且我无法让它与节点请求承诺本地工作。

我曾尝试将我的 XML 字符串转换为缓冲区、String()、.toString 等。如果我将 json:true 设为 POST,则 POST 有效,因此我认为这不是网络问题(当传递带有 json true 的 xml 字符串时,它会发送像 { 'variablename': 'stringed XML that I want to send as body' } )

这是我正在使用的。我一直在敲我的头一段时间在这里任何帮助表示赞赏。

理想情况下是承诺/异步。

也许我应该寻找一个 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);
            })
Run Code Online (Sandbox Code Playgroud)

Dil*_*yle 5

感谢@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
};
Run Code Online (Sandbox Code Playgroud)