how to post formdata or http.context.form data from angular 6 to asp.net mvc web api or api using Token based authentication with header
_____________________________________________________________________
Create form data :-
const formdata = new FormData();
formdata.append('postJson', JSON.stringify(postJson));
_____________________________________________________________________
Attach Form data To Form-data object and call contract service to post data :-
this.PostService.Postdata(formdata).then((res: any) => {
this.loggerService.info(res.status);
this.responseStatus = res.status;
this.responseDraftId = res.result.alfResponse.draftId;
if (res.status === 200) {
this.responseFilePath = res.result ? (res.result.FullFilePath ? res.result.FullFilePath : '') : '';
this.showPopup('success', 'Success!', res.result.Reference + ' File has been created successfully.');
} else {
this.showPopup('error', 'Error!', 'Error while submit Contract!');
}
}).catch(error => {
this.loggerService.error(error);
this.showPopup('error', 'Error!', 'Error while submit Contract!');
});
}
______________________________________________________________________________
create Service "Post Service" with Promises :-
PostService(data: any): Promise<any> {
return new Promise((resolve, reject) => {
this.apiService.postWithformHeader('Contract/GenerateContract', data).subscribe((res: any) => {
return resolve(res);
}, (err) => {
reject(err);
});
});
}
//note apiservice is class name where this method is write
_________________________________________________________________________________
method for form data header Attach with Token :-
postWithformHeader(URL, data) {
this.spinner.displaySpinner(true);
const optionsData: any;
if(localstorage.getkey('mytoken'))
{
optionsData = {
headers: this.createAuthorizationHeader(),
};
}
else{
this.router.navigate(['login']);
}
const response = this.http.post(this.appUrl + URL, data,optionsData).map((res => {
this.spinner.displaySpinner(false);
return res;
})).catch((error: any) => {
this.spinner.displaySpinner(false);
return this.httpErrorHandler.handleError('api-service', error, URL);
});
return response;
}
_________________________________________________________________________________
Method for create header :-
createAuthorizationHeader() {
let headers = new HttpHeaders();
headers = headers.append('authorization', 'Bearer ' + localstorage.getkey('mytoken'));
return headers;
}
________________________________________________________________________________
Thanks for this kind of worthy information. this was really very helpful to me. keep continuing.
ReplyDeleteEducation Franchise India
Spoken English Franchise
Franchise For Spoken English Classes
Top Education Franchise In India
Best Education Franchise In India
Computer Education Franchise
Education Franchise India
Computer Center Franchise
Education Franchise Opportunities In India