There is a sprint boot API, which is configured with authentication, on accessing it, the response will be a Downloadable link.
I tried integrated the same in angular as follows : Basically, using a service file to invoke the rest API, on successful invocation, i am reading the url from the response and , using an anchor tag to append it to document body and click it programmatically ,
constructor(private api: ApiService)
this.api.getLogs(param).subscribe(result =>{
if (result.status == 200) {
var file_path = result['url'];
let a = <HTMLInputElement>document.createElement('A');
$(a).attr("href", result['url']);
$(a).attr("download", file_path.substr(file_path.lastIndexOf('/') + 1));
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
console.log('failed');
}
});
And in the service file, I used httpHeaders to include the username and password while invoking the rest API.
public getLogs(data):Observable<any>{
const url = 'http://<localhost>:<port>:</path>'
return this.http.get(url, {
headers: new HttpHeaders({ 'Authorization': 'Basic ' + btoa('test:test') }),
observe: 'response'});
}
But when this is executed in browser, the app redirects to the url shared by API response and it asks for the authentication. However once i give the credentials, all the following invocations are not asking for this. What is that is missing here, or is there anything i was implementing it in wrong way , let me know.
