Angular 6 - HttpClient.get 401 error response whil

2019-08-05 17:40发布

问题:

I am trying to call a rest api that returns octet stream content in Angular 6.

The request is as follows:

fetchOutput(jobId : string,outputId:string) : {

    const options = {
        headers: new HttpHeaders( {
            'Content-Type': 'application/octet-stream',
            'ContentDisposition' : 'attachment',
            'ContentDisposition.FileName' : 'TEST_OUTPUT.csv' } ),
        responseType: 'blob' as 'blob'
      };

    var endpoint = "https://myHost/api/jobs/1/output/2/?oauth_consumer_key=123&oauth_signature_method=HMAC-SHA1&oauth_nonce=651487208&oauth_timestamp=1551696001&oauth_version=1.0&format=csv&oauth_signature=Em12lsCwiEbsj4="

    return this.httpClient.get(endpoint, options);
}

Below is the call to this rest API:

fetchOutput(jobId,outputId).subscribe((data: any) => {

    let b:any = new Blob([data], { type:  "application/octet-stream" });
    console.log(b);
});

Error that I get is:

error:{ Blob
size: 0
type: "text/xml" }

name: "HttpErrorResponse"

ok: false

status: 401

statusText: "Unauthorized"

Also, please note that when I copy the same endpoint and hit on postman, it works perfectly fine. And the same request when fired via ajax also runs successfully.