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

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;
  }
  ________________________________________________________________________________

7 comments:

  1. You should be a piece of a challenge for probably the best website on the web. I will suggest this site!

    tech news

    ReplyDelete

Thank You For Your Great Contribution

Featured Post

How To Dynamic Upload multiple files with extra detail of model in asp.net core

 How To Dynamic Upload multiple files with extra detail of model in asp.net core  1). firstly create Model File  namespace MyModel {     pub...

Popular Posts