angularpostmanangular-cli

Unable to get authentication token


I'm trying to get authentication token by using the below code but am didnt get any. However, I got token in Postman.

fetchPeople() { 
    //return this.http.post('http://169.38.82.132:94/token',data,{ headers: this.headers }); 
    let url = "http://169.38.82.132:94/token";
    var headers = new HttpHeaders();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Accept', 'application/json');
    let urlSearchParams = new URLSearchParams();
    urlSearchParams.set('grant_type', 'password');
urlSearchParams.set('username', 'user');
urlSearchParams.set('password', 'user');
//let body = urlSearchParams.toString();
const data = 'grant_type=password';
return this.http.post( url, data,
  {headers: new HttpHeaders({
    'Accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded'
  })}  
  )
  };  
}
console.log(data);

Did I missed any logical settings. Please guide me to get authentication token. In console am getting:

grant_type=password&username=user&password=user
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
Live Reloading enabled.
POST http://169.38.82.132:94/token 400 (Bad Request)


Solution

  • fetchPeople() { 
        //return this.http.post('http://169.38.82.132:94/token',data,{ headers: this.headers }); 
        let url = "http://169.38.82.132:94/token";    
    const data = 'grant_type=password&username=user&password=user';
    return this.http.post( url, data,
      {headers: new HttpHeaders({
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      })}  
      ).subscribe(resp => {
        console.log(resp);
    })
      };  
    }
    console.log(data);
    

    The http post is returning a observable, so you can subscribe and process your response