0

I have a contentUrl when user clicks on that,file should be downloaded. If I try to browse the contentUrl in the browser it asks for Userid and password.I believe this is Basic Authentication.I dont want user to enter UserId and Password so I am passing it Authorization(Please see below code). Url looks some thing like this: http://XXX.XXX.XX.XXX:8080/flowable-rest/service/runtime/tasks/90875/attachments/90555/content

let username : string = 'admin';
let pwd : string = 'test';

let headers = new Headers();
headers.set('Accept', 'text/json'); 
headers.append('Content-Type', 'application/json');
headers.set('Access-Control-Allow-Origin', '*');
headers.set('Access-Control-Allow-Methods','POST, GET, OPTIONS, PUT, DELETE');
headers.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
headers.append("Authorization", "Basic " + btoa(username + ":" + pwd)); 
return this.http.get(url, { headers: headers })
    .pipe(map(r => {
        console.log(r);           

    })).pipe(catchError(this.handleError));

I am not 100% sure whether this approach is fine for my requirement.

I tried contentUrl in the PostMan.In PostMan down right corner 'Download' button appeared and asked me to download.Below is the message it says

This response could not be previewed. Download the response to open it with an appropriate application.

If I click on Download my attachment is getting downloaded.

Pro-grammatically is http.get equivalent to Download? If not how should I code it? Basically my requirement is if I click on the link attachment should be downloaded.

PostMan Execution

reddy
  • 173
  • 1
  • 4
  • 9
  • In the IIS server set Anonymous Authentication: Disabled and ASP.NET Impersonation Enabled. This will make the user have to log into the site and then you can pass the credentials through to the other server. – jriver27 Jan 04 '19 at 17:32

1 Answers1

0

Try setting withCredentials: true.

i.e.

// let username : string = 'admin'; //don't do this
// let pwd : string = 'test';   //don't do this
const headerJson = {
    'Content-Type': 'application/json'
};

const headers = new HttpHeaders(headerJson);

return this.http.get(url, { headers: headers, withCredentials: true })
    .pipe(map(r => {
        console.log(r);
    })).pipe(catchError(this.handleError));
jriver27
  • 852
  • 5
  • 19