1

I have a endpoint which returns a csv data set which will be saved using saveas and Blob AngularJS frontend works fine while Angular 8 struggles. Here is my Angular 8 implementation

 const headers = new HttpHeaders({
            Accept: 'text/csv',
        });
        const options = { headers };
        this.httpClient
            .post<ExportCsvSettingsViewDto>(this.appConfigService.buildApiUrl(this.loadPath(batchId)), csvDto, options)
            .subscribe((fileResult: any) => {
                const file = new Blob([fileResult], { type: 'text/csv' });
                saveAs(file, this.fileName + '.csv');
            });

This results in a HttpErrorResponse stating: Object {error: SyntaxError: Unexpected token . in JSON at positio…, text: "1.112.373;en-US Date Time UTC;Date Local Time;Bat…"}

I suspect this is because HttpClient treats the response as JSON instead of "text/csv" Those were the request headers:

Connection: keep-alive
Content-Length: 141
Pragma: no-cache
Cache-Control: no-cache
Accept: text/csv
Content-Type: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

and response headers:

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: text/csv
Expires: 0
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
X-UA-Compatible: IE=edge
Content-Length: 191147

I tried to explicitly set "responseType" in request header to "text/csv" but that's just not possible. Any ideas on that?

1 Answer 1

7

In case anybody cares: My suspicion was right: the response was considered to be of type json. Setting responseType='text' worked by doing like this:

  const options = { headers, responseType: 'text' as any };

I consider this to be a big issue actually

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.