【问题标题】:how to send xml response to a given url from express route?如何从快速路由向给定的 url 发送 xml 响应?
【发布时间】:2020-09-28 08:32:53
【问题描述】:

我需要从快速路由发送一个 XML 响应到作为 POST 请求给出的 url?

const xml = `<sourcedGUID>
             <sourcedId>ASSMT12345</sourcedId>
</sourcedGUID>
<contextID>
    <textString>cls1234</textString>
</contextID>
<userID>
    <textString>usr123</textString>
</userID>`

我在 /status 的 express 路由中,我需要使用 express 将 xml 变量发送到 url:'https://example.com/cli'。

我怎样才能实现这个用例?

【问题讨论】:

    标签: node.js xml express response


    【解决方案1】:

    你需要使用requestaxioshttp之类的东西,我将在这里使用request

    var request = require('request');
    
    request.post({
        url: <the-url-where-to-send-xml>,
        method: 'POST',
        headers:{
            'Content-Type': 'application/xml',
        },
        body: xml
    },
    function(error, response, body){
        console.log(response.statusCode);
        console.log(body);
        console.log(error);
    });
    

    【讨论】:

    • 它会发送到自定义网址吗?或者它将它发送回请求的?
    • 这是你发回请求的内容,如果你添加更多代码我可以更精确
    • 假设有两个 url:https:example1.com 和 https:example2.com 那么,你从 https:example1.com 发送请求并且需要将响应作为 xml 数据发送回 example2.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 2018-01-22
    • 2016-11-16
    • 2017-12-21
    相关资源
    最近更新 更多