how to pass username and password with api in angular? -


i have api. if open u should enter username , password. want data in api how? , if write get("....api-url....") shows unauthorized error. how can pass username , password api?

constructor(private _http: http) {     this.getmyblog(); }  private getmyblog() {     return this._http.get('http://localhost:8088/.../...')         .map((res: response) => res.json())         .subscribe(data => {              this.data = data;             console.log(this.data);          }); } 

i'm assuming want send along request query parameters?

this terrible bad practice (the users password part of url - while body contents may protected ssl, actual url visible attackers) - read more @ https://www.fullcontact.com/blog/never-put-secrets-urls-query-parameters/

also, http module being deprecated - @ using httpclientmodule instead (https://angular.io/guide/http)

if still want this:

public getmyblog(username, password): observable<any> {   const params = new httpparams().set('username', username).set('password', password);   return this.http.get('...apiurl...', { params }); } 

for post:

public getmyblog(username, password): observable<any> {   const body = { username, password };   return this.http.post('...apiurl...', body); } 

a better way requests send token in headers:

public getmyblog(token) {   const headers = new httpheaders().set('authorization', token);   return this.http.get('...apiurl...', { headers }); } 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -