this is an edited post. I am trying this simple http
request from an API with basic auth (like user and pass) and a params object (bind to a form); I've read the docs, checked out several posts, but nothing seem to work; I always get a 401 as a response...
Can some give me a help? (i'm new at this)
This is what I've got:
import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Http, Headers} from '@angular/http';
import {response} from '../../models/response';
import {HttpErrorResponse} from '@angular/common/http';
import {HttpHeaders} from '@angular/common/http';
@Component({
selector: 'app-testing',
templateUrl: './testing.component.html',
styleUrls: ['./testing.component.css']
})
export class TestingComponent implements OnInit {
//OPTIONS
selectedColor:string;
selectedFont:string;
selectedSize:string;
//INTERFACE
results: response;
params = {
"handwriting_id": "8X3WQ4D800B0",
"text": "",
"handwriting_size": "",
"handwriting_color": "",
}
constructor(private http: HttpClient) { }
ngOnInit() {
}
onSubmit(f){
console.log(this.params);
this.http.get<Response>('https://api.handwriting.io/render/png?' + this.params,{
headers: new HttpHeaders().set('7Q0ZBC889M7WRGRM','N6BQK4Z8PZ1HYYT4'),
}).subscribe(data => {
this.results = data['results'];
console.log(this.results);
},(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
console.log('An error occurred:', err.error.message);
} else {
console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
}
});
}
//OPTIONS
changeFont(){
document.getElementById("output-text").style.fontFamily = this.selectedFont;
}
changeColor(){
document.getElementById("output-text").style.color = this.selectedColor;
}
changeSize(){
document.getElementById("output-text").style.fontSize = this.selectedSize;
}
}