This method below can be used in Typescript to build up a query string from a given object of a class instance in Typescript. Use it when performing HTTP(S) GET calls.
BuildQueryString<T>(input: T): string {
let q = "?";
Object.keys(input).forEach(key => q += key + "=" + input[key] + "&");
if (q.length > 0) {
q = q.substr(0,q.length-1);
}
return q;
}
Sample usage in Angular 8 solution of mine for example:
async saveSurvey(): Promise<SurveyItem> {
//debugger
let s: SurveyItem = {
Id: 0, LastUpdate: new Date(),
EquipmentNumber: this.equpimentIdentifier, RoomNumber: this.roomIdentifier,
PrimaryUsage: this.primaryUsage,
IsInspected: this.GetBoolean(this.isInspected), IsNotFound: this.GetBoolean(this.isNotFound)
};
let q = this.fetcher.GetAPIUrl('savesurvey');
q += this.fetcher.BuildQueryString(s);
const response = await this.http.get<SurveyItem>(q, { headers: this.corsHeaders }).toPromise();
//this.openSnackBar('Lagret utstyr ' + this.equpimentIdentifier);
this.openSnackBar('Lagret utstyr ' + this.equpimentIdentifier + ' Laster inn liste for utstyr i samme rom også..');
//After saving survye - update also the properties with updated Id and LastUpdate
this.SetCurrentSurveyItemPropretiesFromSurveyItem(response);
this.onRoomSelected(this.roomIdentifier);
return response;
}
No comments:
Post a Comment