Angular 6 Downloading file from rest api : <p>I have my REST API where I put my pdf file, now I want my angular app to download it on click via my web browser but I got HttpErrorResponse </p>

<p>"Unexpected token % in JSON at position 0"</p>

<p>"SyntaxError: Unexpected token % in JSON at position 0↵    at JSON.parse (</p>

<p>this is my endpoint</p>

<pre><code>    @GetMapping("/help/pdf2")
public ResponseEntity&lt;InputStreamResource&gt; getPdf2(){

    Resource resource = new ClassPathResource("/pdf-sample.pdf");
    long r = 0;
    InputStream is=null;

    try {
        is = resource.getInputStream();
        r = resource.contentLength();
    } catch (IOException e) {
        e.printStackTrace();
    }

        return ResponseEntity.ok().contentLength(r)
                .contentType(MediaType.parseMediaType("application/pdf"))
                .body(new InputStreamResource(is));

}
</code></pre>

<p>this is my service </p>

<pre><code>  getPdf() {

this.authKey = localStorage.getItem('jwt_token');

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/pdf',
    'Authorization' : this.authKey,
    responseType : 'blob',
    Accept : 'application/pdf',
    observe : 'response'
  })
};
return this.http
  .get("http://localhost:9989/api/download/help/pdf2", httpOptions);
</code></pre>

<p>}</p>

<p>and invocation </p>

<pre><code>this.downloadService.getPdf()
  .subscribe((resultBlob: Blob) =&gt; {
  var downloadURL = URL.createObjectURL(resultBlob);
  window.open(downloadURL);});
</code></pre>
