【问题标题】:Can Azure Functions return XML?Azure Functions 可以返回 XML 吗?
【发布时间】:2016-11-10 22:44:41
【问题描述】:

寻找从 Azure 函数返回 XML 的 Node.js 示例。我下面的代码返回 xml 字符串,但响应 Content-Type 设置为 text/plain; charset=utf-8 而不是 text/xml; charset=utf-8

index.js

module.exports = function(context, req) {
    var xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Say>Azure functions!</Say></Response>';

    context.res = {
        contentType: 'text/xml',
        body: xml
    };

    context.done();
};

这里是绑定。

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "disabled": false
}

【问题讨论】:

    标签: xml azure twilio azure-functions


    【解决方案1】:

    标记,

    绝对!您已经很接近了,但您可以看到如何在响应 here 上设置内容类型的示例。

    下一个版本还有一个修复程序,它将启用正确的内容协商,这将消除在许多情况下显式设置该内容的需要。

    【讨论】:

    • 谢谢!添加这个有效:headers: { 'Content-Type': 'text/xml' }
    • 你确定这有效吗?我得到了这个输出:&lt;string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&amp;lt;Response&amp;gt;&amp;lt;Say&amp;gt;Azure functions!&amp;lt;/Say&amp;gt;&amp;lt;/Response&amp;gt;&lt;/string&gt;
    • 如果我使用 text/html 作为内容类型,它就可以工作。 Twilio 将接受这一点,并且 Azure 不会对 XML 字符串进行编码,如我之前的评论中所示。
    • github.com/Azure/azure-webjobs-sdk-script/issues/965 -- 需要在回复中添加isRaw: true
    • 是的。 isRaw 是最近添加的,用于处理 1.0 中所做的一些更改,并且是返回原始有效负载的方式。
    【解决方案2】:

    为了让法比奥的回答更完整,以下是更新后的代码:

    module.exports = function(context, req) {
        var xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Say>Azure functions!</Say></Response>';
    
        context.res = {
            "headers" : { 
                "Content-Type" : 'text/xml'
            },
            "body": xml,
            "isRaw" : true
        };
    
        context.done();
    };
    

    你不需要更改function.json。

    “headers”块可用于设置您想要返回的任何标题。请注意,如果您在 Function App 的设置中设置了任何设置 CORS 数据的内容,则任何与 CORS 相关的标头都将被覆盖。您要么必须在函数应用设置中设置 CORS 数据,要么在代码中手动处理 CORS。

    需要将“isRaw”设置为 true,这样 Azure 函数才不会试图智取您,并且 XML 对您已经编码的数据进行编码。

    仅供参考,根据我的经验,Azure Functions 正在积极开发并经常更改。因此,如果您遇到问题或此代码不再有效,您最好的选择; is to search/open an issue on Github.

    【讨论】:

    • 这个isRaw 非常有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2012-04-18
    相关资源
    最近更新 更多