How to make Azure function output JSON to browsers

I have an azure function setup with this source:

module.exports = function(context, req) {
    //this is the entire source, seriously
    context.done(null, {favoriteNumber : 3});
};

When I use a tool like a postman to visit him, I get a good JSON output, just like I want:

{
  "favoriteNumber": 3
}

The problem is that I visit it in the browser (chrome, firefox, etc.). I see:

<ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><KeyValueOfstringanyType><Key>favoriteNumber</Key><Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:int">3</Value></KeyValueOfstringanyType></ArrayOfKeyValueOfstringanyType>

enter image description here How can I get azure to always give me json output, regardless of request headers?

+4
source share
1 answer

Have you tried setting the response object Content-Typeexplicitly to application\json?

module.exports = function(context, req) {
    res = {
        body: { favoriteNumber : 3},
        headers: {
            'Content-Type': 'application/json'
        }
    };

context.done(null, res);
};

. , Chrome ,

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

XML .

+7

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


All Articles