Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect after a fetch post call

I am creating an social login page with an Access Management (AM) server. When user click on the login button then I make a fetch http post call to AM server. AM server generates a HTTP 301 redirect response with auth cookies to the social login page. I need to follow somehow this redirect response and show the new content in the web browser.

UI: ReactJS

Request:

POST /api/auth/socialauth/initiate HTTP/1.1 Host    example.com User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Accept  */* Accept-Language en-US,en;q=0.5 Accept-Encoding gzip, deflate origin  http://web.example.com:8080 Referer http://web.example.com:8080/myapp/login Cookie  authId=...; NTID=... 

Response

HTTP/1.1 307 Temporary Redirect https://www.facebook.com/dialog/oauth?client_id=...&scope=public_profile%2Cemail&redirect_uri=http%3A%2F%2Fam.example.com%3A8083%2Fopenam%2Foauth2c%2FOAuthProxy.jsp&response_type=code&state=qtrwtidnwdpbft4ctj2e9mv3mjkifqo 

React code:

initiateSocialLogin() {     var url = "/api/auth/socialauth/initiate";      fetch(url, { method: 'POST' })         .then(response => {             // HTTP 301 response             // HOW CAN I FOLLOW THE HTTP REDIRECT RESPONSE?         })         .catch(function(err) {             console.info(err + " url: " + url);         }); } 

How I can follow the redirect response and show the new content in the web browser?

like image 211
zappee Avatar asked Sep 27 '16 22:09

zappee


People also ask

How do you redirect after fetch react?

Use the .push Method Then, wherever we want to cause a redirect in our app, whether that is after a fetch request—or after checking something like whether the use is logged in and authorized—we can simply use our variable + the . push(<route>) method to redirect to whatever route we'd like.

Does fetch follow redirects?

Normally, fetch transparently follows HTTP-redirects, like 301, 302 etc.

Can POST requests be redirected?

in response to a POST request. Rather, the RFC simply states that the browser should alert the user and present an option to proceed or to cancel without reposting data to the new location. Unless you write complex server code, you can't force POST redirection and preserve posted data.

CAN REST API redirect?

In response to a REST API request, the Nest API server returns a redirect. The REST client detects the redirect and requests the page that the client was redirected to. Some HTTP implementations do not forward the Authorization header to the redirected URI, and this results in a 401 Unauthorized error.


1 Answers

Request.redirect could be "follow", "error" or "manual".

If it is "follow", fetch() API follows the redirect response (HTTP status code = 301,302,303,307,308).

If it is "error", fetch() API treats the redirect response as an error.

If it is "manual", fetch() API doesn't follow the redirect and returns an opaque-redirect filtered response which wraps the redirect response.

Since you want to redirect after a fetch just use it as

fetch(url, { method: 'POST', redirect: 'follow'})     .then(response => {         // HTTP 301 response     })     .catch(function(err) {         console.info(err + " url: " + url);     }); 
like image 63
Shubham Khatri Avatar answered Oct 10 '22 10:10

Shubham Khatri