Redirect from post to get

#1

Hi, I am new with node-red.

I have the following URL: (GET) /test/login, (POST) /test/login and (GET) /test/global.
The problem it's how to go from (POST) /test/login to (GET) /test/global.

I tried the following solution but doesn't work: this.

Which could be the problem?

Thanks for all!

0 Likes

#2

Hello again.
I solved the problem using redirect and without creating a new variable after processing the POST.

Redirect code:

msg.res.redirect("/test/login");

Thanks for all

0 Likes

#3

You should not rely on that working in the long term. msg.res.redirect is an internal function that we really shouldn't expose.

There's no reason not to do this properly as per the stack overflow answer.

0 Likes

#4

The problem I have using stack overflow answer it's that it takes long time thinking and doesn't load the page.

These are my nodes:

[{"id":"1b0b4d38.437b6b","type":"http in","z":"49b70d91.d2841c","name":"","url":"/test/login","method":"get","upload":false,"swaggerDoc":"","x":230,"y":140,"wires":[["c9791a3c.1c6cd"]]},{"id":"c9791a3c.1c6cd","type":"template","z":"49b70d91.d2841c","name":"","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<html>  \n  <head>\n      <title>Login</title>\n  </head>\n  <body class=\"bg-primary\">\n    <div class=\"container\">\n        <form action=\"/test/login\" method=\"post\">\n            <div class=\"row\">\n                <label for=\"username\" class=\"offset-md-3 col-md-2 text-light\">Username</label>\n                <input type=\"text\" name=\"username\" id=\"\" value=\"\" class=\"col-md-4 bg-transparent\" />\n            </div>\n            <div class=\"row\">\n                <label for=\"password\" class=\"offset-md-3 col-md-2 text-light\">PW</label>\n                <input type=\"password\" name=\"password\" id=\"password\" value=\"\" class=\"col-md-4 bg-transparent\"/>\n            </div>\n            <div class=\"row\">\n                <input type=\"submit\" class=\"col-md-offset-2 col-md-2 text-light bg-transparent\" value=\"Login\"/>\n            </div>\n        </form>\n    </div>\n  </body>\n</html>","output":"str","x":428,"y":140,"wires":[["f1a3bfc6.bc8e7"]]},{"id":"f1a3bfc6.bc8e7","type":"http response","z":"49b70d91.d2841c","name":"","statusCode":"","headers":{},"x":603,"y":141,"wires":[]},{"id":"82038dad.f03d2","type":"http in","z":"49b70d91.d2841c","name":"","url":"/test/login","method":"post","upload":false,"swaggerDoc":"","x":218,"y":203,"wires":[["88f91e9c.9eb868"]]},{"id":"a575af84.f2ec28","type":"http in","z":"49b70d91.d2841c","name":"","url":"/test/global","method":"get","upload":false,"swaggerDoc":"","x":217,"y":259,"wires":[["1abb80bf.477607"]]},{"id":"1abb80bf.477607","type":"template","z":"49b70d91.d2841c","name":"","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<html>\n    <head>\n        <title>Global</title>\n    </head>\n    <body>\n        <h1>It works</h1>\n    </body>\n</html>","output":"str","x":429,"y":259,"wires":[["1f22cc03.0c0044"]]},{"id":"1f22cc03.0c0044","type":"http response","z":"49b70d91.d2841c","name":"","statusCode":"","headers":{},"x":611,"y":261,"wires":[]},{"id":"88f91e9c.9eb868","type":"function","z":"49b70d91.d2841c","name":"Redirect ","func":"//msg.res.redirect(\"/test/global\");\nmsg.statusCode = 303;\nmsg.headers = {\n    Location: \"http://192.168.0.1:1880/test/global\"\n}\n//return msg;","outputs":"0","noerr":0,"x":422,"y":202,"wires":[]}]
0 Likes