0

I am working with a secured api that requires me to send an Authorisation header on each request, this is working fine. However I have an issue with the handling of 401 responses.

My call to the API is made as follows:

let headers: Headers = new Headers();            
headers.append('Authorization', 'Bearer ' + {myauthtoken});

let request = this.http.get(Utils.getUrl('/somesecuredmethod'), { headers: headers });
        request.subscribe(
            result => { this.setLoginDetails(result); },
            error => {
                this.loginFailed = true;
                this.reasonLoginFailed = error;
                return true;
            });

Within error I can see the response code 401, however I also see the following in the chrome console

GET https://localhost/somesecuredmethod 401 (User not found in database.)

Does anyone know how I can get the message text in angular ?

Thanks, Jon.

EDIT:

I have previously called this same method using pure ajax as below and can get the error text.

$.ajax({
    url: baseurl + "/somesecuredmethod",
    method: "get",
    headers: { 'Authorization': 'Bearer ' + accessToken }
}).done(function (data) {
    // do something with the success
}).fail(function (jqXHR, status, err) {
    $("#loading_application").addClass("hidden");
    $("#login_failed_reason").text(err);        <--- here I can the error text shown in the dev console
    $("#login_failed").removeClass("hidden");
});
ciantrius
  • 683
  • 1
  • 6
  • 21

1 Answers1

0

If it's a text message in the payload, you could the text method on the error:

let request = this.http.get(Utils.getUrl('/somesecuredmethod'), { headers: headers });
request.subscribe(
  result => { this.setLoginDetails(result); },
  error => {
    this.loginFailed = true;
    this.reasonLoginFailed = error.text(); // <-----
    return true;
  });
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Sorry, this didn't work. the text method returns undefined. I have just captured the request in fiddler and the error text is contained within the headers. – ciantrius May 06 '16 at 11:40