Search code examples
angularangular-httpclient

Angular HttpClient response is null but network tools show response


Working on an Angular app that I'm updating from 4.x to 5, and I have a service that calls to the server, but the response comes in as null when the network pane in dev tools shows the expected response.

Here is the service method:

public ExportPendingInvoicesReport(data: InvoicePaginationRequest){
    const link = AppSettings.API_REPORTS_EXPORT_PENDING_INVOICES_REPORT;
    const params = this.mapRequestParameters(data);

    console.log(params.keys());

    this.httpClient.get<string>(link, {params}).subscribe((response: any) => {
        DownloadHelper.DownloadFile('PendingInvoices.csv', 'CSV', response._body);
    })
}

mapRequestParameters returns a valid HttpParams object, and the network dev tools shows this as the response: DevTools Response

But in the console, I'm getting a null value for the response object. Why is this happening?

Updated with current code.


Solution

  • Call with the parameter option { responseType: 'text' }.

    this.httpClient.get(link, {params: params, responseType: 'text'})
    

    This ways the response body is not treated as json.