Search code examples
angularrxjsrxjs-pipeable-operators

How To Update values in HttpParams in Angular Post Request with rxjs Observable


I have a polling service in Angular 13 which pulls data from an external server using rxjs interval. I'm able to get the data alright in the specified interval. The problem is that I'm unable to update the httpParams with a new set of data before the next call. The params contain a set of IDs used as SQL constrainers.

  getAllCurrencies(id, code): Observable<any> {
  this.bookingCode = code;
  this.bookingId = id;  
  let url = 'someurl';
  let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
  let body = new HttpParams()
  .set('booking_code', this.bookingCode)
  .set('booking_id', this.bookingId)
  .set('token', this.token); 
  return this.allCurrencies$ = interval(5000).pipe(
  mergeMap(() =>
    this.http.post<any>(this.GLOBAL_URL + url, body, { headers: headers }).pipe(
      map((res) => {
         let result:any = [];
        if(res.data.length > 0){   
          this.testingOne();         
            this.bookingId = res.data[0].id;
            result = res.data;
        }
        return result;
      }))),retry(),share(),takeUntil(this.stopPolling)
  );
 }

I want to update the value of this.bookingId in .set('booking_id', this.bookingId) when a new value is returned and then continue the call.


Solution

  • Try something like below, it should work

    map((res) => {
      let result:any = [];
      
      if(res.data.length > 0){   
        this.testingOne();         
        this.bookingId = res.data[0].id;
    
        body = body.set('booking_id', this.bookingId);
        result = res.data;
      }
      return result;
    })