0
votes

i am trying to export a simple list of values returned from an api to csv. the response content type is application/octet-stream and the values are as shown below: I want to export the registration_time to date value.

name, email, age_gate, access_mode, city, state, registration_time
Lisa,lisa@facebook.com,3,NATIVE_IOS,Bryan,Connecticut,1631583824
Luke,luke@aol.com,3,NATIVE_IOS,Norfolk,Nebraska,1625565926
Bruce,bruce@gmail.com,3,NATIVE_ANDROID,Corona,West Virginia,1634430136
Tabitha,tabitha@aol.com,3,NATIVE_IOS,Scottsdale,Montana,1634552823
Samuel,samuel@outlook.com,3,WEB,Allentown,Illinois,1624695790
Natasha,natasha@outlook.com,3,WEB,Naples,Texas,1625381700
Jaclyn,jaclyn@aol.com,1,NATIVE_ANDROID,Portland,Georgia,1634500013
Reginald,reginald@outlook.com,1,NATIVE_IOS,Fort Smith,Oklahoma,1625287277

I use the following code in angular to export to csv:

service:

exportToCSV(): Observable<any> {
    const httpOptions: any = {
      observe: 'response',
      headers: this.getHeaders(),
      responseType: 'arraybuffer'
    }

    return this.http.get(url, httpOptions)
    .pipe(
      catchError((response: HttpResponse<null>) => {
        return this.getError(response);
      })
    )
  }

component:

exportToCSV(): void {
    this.userEmailListService.exportToCSV()
    .pipe(first())
    .subscribe((response: any) => {
      const contentType = response.headers.get('content-type');
      const blob = new Blob([response.body], { type: contentType });
      const fileName = 'UserAndEmails.csv';
      const file = new File([blob], fileName, { type: contentType });
      saveAs(file);
    })
  }