Azure features: NodeJS - HTTP response is displayed as XML, not HTTP response

I have an Azure function written in NodeJS where I am trying to call an HTTP redirect from 302. The documentation is very sparse regarding valid entries in the response. As a result, I created an object with what, in my opinion, should be correct in order to generate a redirect, but all I get is an XML response. Even elements, such as a status code, are displayed in XML, and do not change the actual status code.

What am I doing wrong?

My code is:

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

This is the answer I get in the browser:

 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> 
+5
source share
2 answers

The problem is that you are not defining the "body" in the answer. This can be set to null, but for Azure functions to be set to its correct representation.

eg. Update your code to:

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

Then you will get the desired answer:

 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 

Edited 2/16/2017 - Using "null" for the body is currently causing an error on Azure. As a result, the response has been updated to use {} instead.

+5
source

This is a bug with azure functions, see the contents of the headers .

Currently, a workaround is to remove any content related headers if your response body is null.

0
source

Source: https://habr.com/ru/post/1262963/


All Articles