2

I want to make non-json request to server using angular 6 HttpClient (@angular/common/http) to get Octet-stream . Below is my code.

getFile(file: any) {
    let headers: new HttpHeaders({
        'Content-Type':  'application/octet-stream',
        'Accept':'application/octet-stream',
        'Authorization': 'Bearer ' + data.token
    })

    return this.http.get<any>(this.baseUrl + '/getfile/'+file, headers);
}

But this gives me JSON parse error.

"HttpErrorResponse", message: "Http failure during parsing for..... ERROR {…} ​ error: {…} ​​ error: SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data"

Whats the way to read it as non-json?

hlovdal
  • 26,565
  • 10
  • 94
  • 165
Manu Mohan
  • 1,694
  • 2
  • 20
  • 31
  • `this.http.get(..., { responseType: 'text' })` may be what you need. What are you going to use the result for? – Explosion Pills Sep 20 '18 at 12:46
  • I want to download it as a file. ALso I'm passing authorization through header. – Manu Mohan Sep 20 '18 at 12:48
  • Possible duplicate of [Angular HttpClient "Http failure during parsing"](https://stackoverflow.com/questions/46408537/angular-httpclient-http-failure-during-parsing) – Kirk Larkin Sep 20 '18 at 14:00

1 Answers1

7

Try this:

let headers = new HttpHeaders({
    'Authorization': 'Bearer ' + data.token
    });
return this.http.get<any>(this.baseUrl + '/getfile/'+file, {headers:headers, responseType: 'blob'});
David
  • 33,444
  • 11
  • 80
  • 118