I am creating a project that takes a GET call from an API and writes out select data from the result to a text file. I understand how to call the API and to print out the select data to the console and the page itself, but I am struggling with putting that data into a text file. Here is the typescript class I have to call the API
export class LoginComponent implements OnInit {
url = '';
login = '';
pass = '';
token = '';
dataToToken = '';
loginPassed = false;
response: any;
constructor(private http: HttpClient) {
}
ngOnInit(): void{
}
onCreateData(){
this.loginPassed = true;
if(this.token === ''){
this.dataToToken = 'Basic ' + btoa(this.login + ':' + this.pass);
}
else if (this.login === ''){
this.dataToToken = this.token;
}
else{
this.dataToToken = this.token;
}
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': this.dataToToken
})
};
this.http.get(this.url, httpOptions).subscribe((response) => {
this.response = response;
});
}
}
And here is my HTML code for printing out the data I want to the page:
<p>URL:</p>
<input
type="text"
class="form-control"
[(ngModel)]="url">
<br>
<br>
<p>Username:</p>
<input
type="text"
class="form-control"
[(ngModel)]="login">
<p>Password:</p>
<input
type="password"
class="form-control"
[(ngModel)]="pass">
<br>
<h4>OR:</h4>
<br>
<p>Token:</p>
<input
type="text"
class="form-control"
[(ngModel)]="token">
<br>
<br>
<button
class="btn btn-primary"
(click)="onCreateData()">Submit</button>
<hr>
<ng-container *ngIf="loginPassed">
<p>Data from API:</p>
<p *ngFor="let lines of this.response"> {{ lines.line1 }} <br> {{ lines.line2 }}</p>
</ng-container>
How do I take the data I collected (line1 and line2) and put them into a downloadable text file?