Search code examples
angularionic4

How can a variable be passed in http URL request in ionic 4 with angular?


How to pass variable as parametre in a http request in ionic 4 with angular?


 response: any;

 constructor(private http: HttpClient) { }

async logIn(Companyid: string, Userid: string, Password: string, lat: any, lot: any, deviceid: string) {


 console.log(Companyid, Userid, Password);
 this.http.get('https://www.service.com/App_1.3.5/login.php',
)
  .subscribe(data => {
   this.response = data;
   });

   // console.log(this.response);

   // return this.response
 }
}

I tried as below but its passing as string

export class UrlService {

  response: any;

  constructor(private http: HttpClient) { }

 async logIn(Companyid: string, Userid: string, Password: string,
 lat: any, lot: any, deviceid: string) {

  // tslint:disable-next-line:whitespace
  // const httOptions = ;
  console.log(Companyid, Userid, Password);
  // tslint:disable-next-line:max-line-length
  this.http.get('https://www.service.com/App_1.3.5/login.php?compid=Companyid&username=Userid&password=Password&deviceID=deviceid&latitude=lat&longitude=lot')
   .subscribe(data => {
    this.response = data;
    });

    // console.log(this.response);

    // return this.response
  }
}

So plz help me with how to pass variables as parameters in a http URL request


Solution

  • You can construct the url with the parameters before passing it to your request:

    const baseURL = 'https://www.service.com/App_1.3.5/login.php?';
    const queryOptions = 'compid=' + Companyid + '&username=' + Userid; //Keep going
    let requestUrl = `${baseURL}${queryOptions}`;
    
     this.http.get(requestUrl)
      .subscribe(data => {
       this.response = data;
     });
     }