0
votes

I have a single user login system. In the entire org and in all application credential is same. When I am sending login request from angular getting below response :

"Status Code{\"statusCode\":200,\"body\":\"\",\"headers\":{\"date\":\"Thu, 13 Sep 2018 16:04:46 GMT\",\"x-powered-by\":\"Servlet/3.0\",\"content-length\":\"0\",\"set-cookie\":[\"LtpaToken2=UXVOlmR+cut/Qu/cxxxmWaEOEkCtxxxq22GgCI4Iz8nffc1n8O1l5AE0sUCPQfZ+nB9vXS/WMkzxanOexJHzJZb4IgqoxTc0UztF+1amzVysdvdBSSIoblF8fFc9iRMkjYPbxZISg29sU1Q6Au0+Dh50ARY1NhkRQ8iwVst1X6Pbc3Dn4YpLO08utqAi9+35Pun+IFJnbTF6BRvleLKXDifZzwmuZlRKs5gseb9GELQYjTLlXFykm8aq0Ulh0Z8VT/xi0uRFFiR0eUrEvo58sev/3XPYv10//ZTkWOqFnADdzhsBoW4wtWVjlFcvlB/24TBaKtNWmYJd3/tsnoRCWPn1yd4udC0YDFsr3h5SGAjgCRMg7gH8+hvpnIiKzzfcxNmQTPlRjav+SUkBfIxAQ5gmGOmDXDcuzMSbx8zJk1RGNcM/Ksmrcv+SOoDMtmCDHWWqTVn9VLPAnF9bZE7Fkmis7QcNxK5UAoMTCCzgfwMEwHeWm4fY7DblWi8RM=; Path=/\",\"JSESSIONID=0000LsRRstmAD-Y923str-8:-1; Path=/\"],\"expires\":\"Thu, 01 Dec 1994 16:00:00 GMT\",\"cache-control\":\"no-cache=\\"set-cookie, set-cookie2\\"\",\"content-language\":\"en-US\",\"connection\":\"close\"},\"request\":{\"uri\":{\"protocol\":\"http:\",\"slashes\":true,\"auth\":null,\"host\":\"abcd.com\",\"port\":80,\"hostname\":\"abcd.com\",\"hash\":null,\"search\":null,\"query\":null,\"pathname\":\"/hello/rest/login\",\"path\":\"/hello/rest/login\",\"href\":\"http://abcd.com\"},\"method\":\"GET\",\"headers\":{\"authorization\":\"Basic r8htMGw5adkhobSMzYXVn\"}}}"

I am getting undefined when I am trying to access statuscode in my login component,

this.apiDataService.getLogin(user).subscribe((data: any) => {
      if (data.statusCode==200) {
        this.apiDataService.storeUserData(data.headers, data.authorization);
        this.router.navigateByUrl('/validate');
      } else {
        this.loginError = true;
        this.errMsg = 'Invalid username/password';
      }

please suggest me how to get statuscode and what should I use for session management, like set-cookie i am not able to use. });

2
please edit your question and put a readable response of getLogin api - Fartab
because you are receiving your response as string, you need to parse it, may be with JSON.parse - Fateh Mohamed
If you are using HttpClient then - by default - in response you have access only to the response body. If you want to access whole response object check this solution stackoverflow.com/a/46809000/8531463 - Karol Trybulec
There is a parse erron in the piece where there is "cache-control":"no-cache=" set - cookie, ... may be you miss a comma - Gianluca Conte

2 Answers

2
votes

Try data.json() to parse the response as JSON object,

this.apiDataService.getLogin(user).subscribe((data: any) => {
  var response=data.json();
  if (response.statusCode==200) {
    this.apiDataService.storeUserData(response.headers, response.authorization);
    this.router.navigateByUrl('/validate');
  } else {
    this.loginError = true;
    this.errMsg = 'Invalid username/password';
  }
1
votes

Try this:

this.apiDataService.getLogin(user).map((res: any) => res.json()).subscribe((data: any) => {
      if (data.statusCode==200) {
          this.apiDataService.storeUserData(data.headers, data.authorization);
          this.router.navigateByUrl('/validate');
      } else {
          this.loginError = true;
          this.errMsg = 'Invalid username/password';
      }