I have set up a Java-based API on a server, with the URL being "ex.com".
This API has an endpoint that returns a PDF file, with the URL set as "ex.com/pdf".
For this endpoint, a POST request is required with a parameter specifying the requested PDF, like so: params = { location: "report.pdf"}.
My goal is to use Angular's Http.post observable to retrieve the PDF file. However, I keep encountering an HttpErrorResponse with the message "Http failure during parsing for http://ex.com/pdf... Unexpected token % in JSON at position 0 at JSON.parse". Since the response is a PDF, I don't want it to be parsed as JSON.
Here is the code I am currently using:
params = {location: "report.pdf"};
return this.http.post<Response>("http://ex.com/pdf", params)
.subscribe(
data => {
var file = new Blob([data], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
}
);
I have confirmed that the endpoint works correctly with Postman, as I am able to successfully use the "Send and Download" option to obtain the PDF file. I just need to figure out how to achieve the same result within Angular. Any help would be greatly appreciated.