Azure函数:NodeJS - HTTP响应呈现为XML而不是HTTP响应

时间:2017-01-18 01:22:10

标签: node.js xml azure response azure-functions

我有一个用NodeJS编写的Azure函数,我试图使用302进行HTTP重定向。文档对于响应中的有效条目非常稀疏。结果我创建了一个对象,我觉得应该是生成重定向的正确条目,但我得到的只是一个XML响应。甚至像状态代码这样的项目也会显示在XML中,而不是更改实际状态代码。

我做错了什么?

我的代码:

module.exports = function(context, req){
    var url = "https://www.google.com";
    context.res =  {
        status: 302,
        headers: {
            Location: url
        }
    }
    context.done();
}

这是我在浏览器中收到的回复:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 1164
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 18 Jan 2017 00:54:20 GMT
Connection: close

<ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><KeyValueOfstringanyType><Key>status</Key><Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:int">302</Value></KeyValueOfstringanyType><KeyValueOfstringanyType><Key>headers</Key><Value i:type="ArrayOfKeyValueOfstringanyType"><KeyValueOfstringanyType><Key>Location</Key><Value xmlns:d5p1="http://www.w3.org/2001/XMLSchema" i:type="d5p1:string">https://www.google.com</Value></KeyValueOfstringanyType></Value></KeyValueOfstringanyType></ArrayOfKeyValueOfstringanyType>

2 个答案:

答案 0 :(得分:5)

问题在于你没有定义&#34; body&#34;在回应中。这可以设置为null,但必须为Azure函数设置它才能正确解释它。

e.g。将您的代码更新为:

module.exports = function(context, req){
    var url = "https://www.google.com";
    context.res =  {
        status: 302,
        headers: {
            Location: url
        },
        body : {}
    }
    context.done();
}

然后您将得到所需的回复:

HTTP/1.1 302 Found
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 0
Expires: -1
Location: https://www.google.com
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 18 Jan 2017 01:10:13 GMT
Connection: close

编辑于2017年2月16日 - 使用&#34; null&#34;对于身体当前在Azure上引发错误。因此,答案已更新为使用{}。

答案 1 :(得分:0)

这是azure函数的错误,请参阅此content headers issue

目前,解决方法是在响应正文为空时删除任何与内容相关的标头。