Share_Improve
Share_Improve

Reputation: 447

Redirecting from POST not working

I have created an API using node and exposed one method which will be executed upon POST request.

From that method I am returning

res.redirect(redirectUrl); 

to redirect to a url.

But when this response is sent back to the browser, it's redirecting again to the API as POST request, looking for the redirectUrl under the API itself.

It might be some stupid mistake but I can't get rid of this. Any help is much appreciated

Upvotes: 0

Views: 1397

Answers (3)

Share_Improve
Share_Improve

Reputation: 447

I found the mistake, it was simply the procotol missing from the redirect URL

Upvotes: 0

Prakash
Prakash

Reputation: 336

The HTTP response status code 302 Found is a common way of performing a redirection.

An HTTP response with this status code will additionally provide a URL in the Location header field. The User Agent (e.g. a web browser) is invited by a response with this code to make a second, otherwise identical, request, to the new URL specified in the Location field. The HTTP/1.0 specification (RFC 1945) defines this code, and gives it the description phrase "Moved Temporarily".

Source : Wikipedia

res.statusCode = 302;
res.setHeader("Location", '/Url');
res.end();

Upvotes: 1

Syed Ayesha Bebe
Syed Ayesha Bebe

Reputation: 1448

Have a look at How do I redirect to another page like google using Nodejs?. I answered for this question. You can redirect using post method with the api.After data you posted response will be redirected to which you want to redirect.

Look at this code

url.route('/num')
.post(function(req,res){
      shortUrl.create(req.body,function(err,url){
if (err) return console.log(error); 
        //return res.send(url);
        res.redirect('https://www.google.com');
     });

})

Hope this helps.

Upvotes: 0

Related Questions