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

Dou*_*oug 5 xml response azure node.js 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();
}
Run Code Online (Sandbox Code Playgroud)

这是我在浏览器中得到的响应:

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>
Run Code Online (Sandbox Code Playgroud)

Dou*_*oug 5

问题是你没有在响应中定义"正文".可以将其设置为null,但必须为Azure函数设置它才能正确解释它.

例如,将您的代码更新为:

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

然后,您将获得所需的响应:

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
Run Code Online (Sandbox Code Playgroud)

编辑于2017年2月16日 - 对于正文使用"null"当前在Azure上引发错误.因此,答案已更新为使用{}.