I want one of my Azure Functions to do an HTTP Redirection.
This is the current code of the function:
module.exports = context => {
  context.res.status(302)
  context.res.header('Location', 'https://www.stackoverflow.com')
  context.done()
}
But it does not work.
A request sent from Postman shows the response has:
Status: 200Location not setIs this correct code? Or is it simply not allowed by Azure Functions?
The code above actually does work, unless you have your binding name set to $return, which is what I assume you have now (you can check in the integrate tab)
Either of the following options will also do what you're looking for
Assuming $return in the binding configuration:
module.exports = function (context, req) {
var res = { status: 302, headers: { "location": "https://www.stackoverflow.com" }, body: null};
context.done(null, res);
};
Or using the "express style" API (not using $return in the binding configuration):
module.exports = function (context, req) {
context.res.status(302)
            .set('location','https://www.stackoverflow.com')
            .send();
};
                        The following code works for me:
module.exports = function (context, req) {
    res = {
        status: 302,
        headers: {
            'Location': 'https://www.stackoverflow.com'
        },
        body: 'Redirecting...'
    };
    context.done(null, res);
};
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With