简体   繁体   中英

How to pass userId to headers in Angular

I'm trying to pass the userId to the headers in this call:

api.service.ts

onUpdateJamItMessageBoard( dispMsg: any, dispStartTs: any, dispEndTs: any, userId: any) {
    const url = this.updateJamItMessageBoard + 'messages';
    const params = new HttpParams()
      .set('dispMsg', String(dispMsg))
      .set('dispStartTs', String(dispStartTs))
      .set('dispEndTs', String(dispEndTs));
      const headers = new HttpHeaders({
        'userId': String (userId)
      });


return this.http.post<JamIt>(url, null, {
  params: params, headers: headers
  });
}

I'm subscribing to it here:

jamit.component.ts

onUpdateJamIt() {
    let dispMsg = this.jamItMessageForMessageCenter + ' ' + this.jamItMessage + ' ' + this.freeText;
    let dispStartTs = this.startTimer;
    let dispEndTs = this.endTimer;
    let userId = '';

    this.apiEndPointService.onUpdateJamItMessageBoard(dispMsg.trim(), dispStartTs.trim(), dispEndTs.trim(), userId.trim()).subscribe(response => {
      console.log(response)
    });

  }

and here's where I use it in the HTML:

jamit.component.html

<button mat-raised-button color="warn" (click)="onUpdateJamIt()">Update</button>


When I click on the click event, I pass all the other parameters except the UserId and I don't know why. Please help. Am I doing it wrong? Or is there something I'm missing?

Try this -

const headers = new HttpHeaders();
headers.set('userId', userId);

For more information check out here -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM