0

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. });

4
  • please edit your question and put a readable response of getLogin api Commented Sep 14, 2018 at 9:23
  • 1
    because you are receiving your response as string, you need to parse it, may be with JSON.parse Commented Sep 14, 2018 at 9:24
  • 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 Commented Sep 14, 2018 at 9:42
  • There is a parse erron in the piece where there is "cache-control":"no-cache=" set - cookie, ... may be you miss a comma Commented Sep 14, 2018 at 9:48

2 Answers 2

2

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';
  }
Sign up to request clarification or add additional context in comments.

Comments

1

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';
      }

4 Comments

I tried this one but getting below error, trying to fix it TypeError: res.json is not a function. But I found this one: stackoverflow.com/questions/46630893/…
Are you using httpClient or http? Can you please post the code of getLogin funciton?
I am using httpclient. import { HttpClient,HttpHeaders} from '@angular/common/http'; getLogin(user){ let headers = new HttpHeaders() headers = headers.append('Content-Type', 'application/json'); console.log("Inside Get Login Details service"); return this.http.post('localhost:3000/app/login', user,{headers}); }
I have fixed the issue as I modified some changes in server side so I get json, i was getting string value. That caused the issue. Thank you sir for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.