Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle 302 in angular 2

I am getting error with status 302 But while trying to log error in catch I am getting 200

post(url, data, successCallBack, errCallback) {
        return this.http.post(apiDomain + url, JSON.stringify(data), {
            headers: this.headers
        }).catch(this.handleError).subscribe(
            (res) => {
                successCallBack(res.json());
            },
            (err) => {
                errCallback(err);
            }
            );
    }

private handleError(error: any) {
    let errMsg = (error.message) ? error.message :
        error.status;
    console.log(error.status); // log is 200
    console.log(error)
    console.error(errMsg);
    return Observable.throw(errMsg);
}

Requirement I want to send another post call on redirect URL redirects. How to get Redirect URL.

Need help.

like image 828
Arun Tyagi Avatar asked Jun 22 '16 11:06

Arun Tyagi


People also ask

Is it possible to intercept 302 request in angular?

This shouldn't work, as the browser will intercept 302 and honor it before Angular gets the response. Angular will get the response from the final location, which is what the OP doesn't want. @DavidAmmouial Why browser will do a redirect for background XMLHttpRequest?

Can @angangular2 handle 302 request?

Angular2 should be able to handle this behavior because 302 is used in Auth2 standard. Please tell us about your environment: Angular version: 2.4.0 Language: [ TypeScript 2.1]

How to avoid getting a 302 status code when redirecting?

The correct solution is to change the server side code to not return 302 status codes because browser kicks in the redirect before Angular (or any SPA for that matter) can do anything about it. Usually, you want to return 401/403s for this very purpose.

Can I use put with angular HTTP client?

Just like in the case of GET, we can also use the Angular HTTP Client to do all the other available HTTP methods, namely the methods typically used for data modification such as PUT. The PUT method should only be used if we want to fully replace the value of a resource.


1 Answers

Late answer I know, but for anyone stumbling across this.

The short answer is you can't as the browser handles 302's itself and won't tell angular anything about that. What you can do is set-up an interceptor style class that monitors what is going on.

Google for angular2 http interceptor or similar, it's a little beefier than your example above and can monitor every XHR connection. An example is here:

https://www.illucit.com/blog/2016/03/angular2-http-authentication-interceptor/

What this now allows is that any connection will come through your interceptor. As we won't be able to monitor 302s, we have to think about what might happen. For example in my example the request suddenly changes the url to something with my auth in it.

Great so my 1st bit of pseudo code would be:

if (response.url.contains('my-auth string')) {
   redirect....
}

I can also see on the headers provided that instead of application/json I've suddenly gone to text/html. Hmm, that's another change I can check for:

if (response.url.contains('my-auth string') && response.headers['content-type'] == 'text/html') {
   redirect....
}

You may have other parameters you can check, however these were good enough to detect a redirect for me. Admittedly this is with respect to being redirected to login and not another example, hopefully you get enough distinct changes check for you to decide whether you have got a 302.

like image 55
PeterS Avatar answered Nov 17 '22 21:11

PeterS