Request:
component.ts
getRevenueReport() {
const revenueReport = {
dateFrom: '1/04/2019',
dateTo: '23/04/2019',
};
this.apiService.getRevenueReport(revenueReport).subscribe( response => {
console.log('response: ', response);
const mediaType = 'application/pdf';
const blob = new Blob(response, { type: mediaType });
saveAs(blob, 'revenue.pdf');
}, error => {
console.log('error: ', error);
});
}
service.ts:
getRevenueReport(revenueReport): any {
const options = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
Accept: '*/*',
Authorization: 'apiKey 8989jjjhjhgghghg765756',
})
};
return this.http.post(this.BASE_API_URL + '/api/report', revenueReport, options);
}
I am trying to download an PDF. I got the error
error: SyntaxError: Unexpected token % in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:13601:51) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2781:31) at Object.onInvokeTask (http://localhost:4200/vendor.js:59081:33) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2780:60) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:4200/polyfills.js:2553:47) at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:2856:34) at invokeTask (http://localhost:4200/polyfills.js:4102:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:4139:21) message: "Unexpected token % in JSON at position 0" stack: "SyntaxError: Unexpected token % in JSON at position 0↵ at JSON.parse ()↵ at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:13601:51)↵ at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2781:31)↵ at Object.onInvokeTask (http://localhost:4200/vendor.js:59081:33)↵ at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2780:60)↵ at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:4200/polyfills.js:2553:47)↵ at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:2856:34)↵ at invokeTask (http://localhost:4200/polyfills.js:4102:14)↵ at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:4139:21)" proto: Error text: "%PDF-1.5↵%����↵3 0 obj↵<
{responseType: 'blob'}
in the request options.See if this helpful: stackoverflow.com/questions/53758280/… – Ashish Ranjansubscribe()
since you're sending apost
request (unless you use{observe: 'response'}
in the request options). What is printed in the console as the response? – Jojofoulk