user2976917
user2976917

Reputation: 66

POST request to receive redirect on client side

I have a service running that receives a POST request and returns a redirect. I have been able to test this using Postman and nghttp2.org (httpbin.org).

On postman, when I POST:

https://nghttp2.org/httpbin/redirect-to?url=https%3A%2F%2Fwww.google.com

The html received is google.com homepage

However to run this on a client side webpage I have the following code:

<html>
    <script type="text/javascript">

        function redirect() {
            fetch('https://nghttp2.org/httpbin/redirect-to?url=https%3A%2F%2Fwww.google.com', {
                headers: { 
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Accept': 'text/html',
                    'Access-Control-Allow-Origin': '*'
                },
                method: 'POST', 
                mode: 'no-cors',
                redirect: 'follow',
            })
        }

        redirect();

    </script>
</html>

I am running this webpage on a server, and it is not redirecting.

Is it possible to get the page to redirect automatically when this webpage is loaded? Or is there another approach I could make. I do need the request to be a POST and I do need the redirect to happen on client side due to cookie handling.

Cheers in advance!

Upvotes: 0

Views: 1034

Answers (1)

Roopam Garg
Roopam Garg

Reputation: 36

Fetch doesn't directly redirect to another page requested by the server. But it can tell you that the server is requesting for redirect to another page and you can handle that accordingly. You can find more info on https://developer.mozilla.org/en-US/docs/Web/API/Response/redirected

Upvotes: 2

Related Questions