Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular SyntaxError: Unexpected token P in JSON at position 0

I'm trying to download an xls file with my angular application, this has worked before angular 8 and suddenly doesn't work anymore. Here is what I have so far:

  export(selectedUsers: any) {
    this.getReport(selectedUsers).subscribe(data => this.downloadFile(data));
  }

  private getReport(selectedUsers: number[]): Observable<Blob> {
        const headers = new HttpHeaders({'content-type' : 'application/json',
                                'Access-Control-Allow-Origin' : '*',
                                responseType : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
    return this.http.post<Blob>(this.globals.api + 'persoon/export', selectedUsers, {headers, 'blob'});
  }

  private downloadFile(data: Blob) {
    console.log('downloading...');
    const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    const url = window.URL.createObjectURL(blob);
    window.open(url);
  }

In the angular documentation it mentioned something like HttpEvent<Blob>, I also tried this but it didn't work. I tried out this similar question: HttpClient - How to request non-JSON data But the solution did not work either since it basically said that it didn't accept the responseType in the given position.

I also checked the response headers of my request, the content-type is also application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Here are my imports:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Mail } from './models/mail';
import { Klant } from './models/klant';
import { GlobalsService } from './globals.service';
import { Observable } from 'rxjs';

Can anyone point me in the right direction and help me solve this issue?

My console error The console error NOTE

I got another issue after applying the accepted solution:

the expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'

For this you should go here: Types of property 'responseType' are incompatible

like image 912
Berend Hulshof Avatar asked Mar 31 '26 05:03

Berend Hulshof


1 Answers

As mentioned in the post you referenced. The responseType: 'blob' is not an header but an option in the request. so your request should look like:

  private getReport(selectedUsers: number[]): Observable<Blob> {
    const headers = new HttpHeaders({
      'content-type' : 'application/json',
      'Access-Control-Allow-Origin' : '*'
    });
    return this.http.post<Blob>(this.globals.api + 'persoon/export', selectedUsers, {headers, ResponseType: 'blob'});
  }
like image 145
Napinator Avatar answered Apr 02 '26 19:04

Napinator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!