4

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: 200
  • Location not set

Is this correct code? Or is it simply not allowed by Azure Functions?

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
kube
  • 13,176
  • 9
  • 34
  • 38
  • A better place to ask might be opening an issue on https://github.com/Azure/azure-webjobs-sdk-script – David Ebbo Mar 21 '17 at 16:09
  • That's the first time somebody tells me to open an issue instead of asking a question on SO. :) In fact I wanted to know if this was correct code, and if it was allowed by Azure Functions before creating an issue on Github. – kube Mar 21 '17 at 16:12
  • @kube, the code is indeed correct (as I mentioned below). I suspect you didn't see the behavior you expected because you were using $return as the binding name. I have more details below. – Fabio Cavalcante Mar 21 '17 at 18:14

2 Answers2

12

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();
};
Fabio Cavalcante
  • 12,328
  • 3
  • 35
  • 43
  • Thanks for the details. It works with your first solution. But I don't know how to make your second solution work though. Just removing the binding to `$return` (and leaving it empty) does not make it work. – kube Mar 22 '17 at 00:46
  • This works fine but one thing to notice that make sure the redirect URL having the protocol, otherwise it will be assumed as https. As least this is happening for our azure function. – Developer Sheldon Mar 08 '19 at 20:49
  • 1
    In my scenario, the second suggestion, the "express style", that redirected like I needed. – user2590928 Mar 11 '19 at 19:55
  • Hi @Fabio, thanks for the response, it work for me in localhost, but when perform a deploy don't work on my production server (the same code). Is necessary to move some other configuration on the server? Thanks in advance. – CristianOrellanaBak Jun 24 '22 at 21:28
3

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);
};
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107