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?

3
  • this.http.get(..., { responseType: 'text' }) may be what you need. What are you going to use the result for? Commented Sep 20, 2018 at 12:46
  • I want to download it as a file. ALso I'm passing authorization through header. Commented Sep 20, 2018 at 12:48
  • Possible duplicate of Angular HttpClient "Http failure during parsing" Commented Sep 20, 2018 at 14:00

1 Answer 1

7

Try this:

let headers = new HttpHeaders({
    'Authorization': 'Bearer ' + data.token
    });
return this.http.get<any>(this.baseUrl + '/getfile/'+file, {headers:headers, responseType: 'blob'});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.