Hello,
I’m trying to set headers to my api request cause my server is password protected. I’ve ran into a lot of posts but didn’t find out what is going wrong with mine.
Here is my home.ts
createAuthorizationHeader(headers: Headers) {
let username = "test";
let password = "test";
headers.append('Authorization', 'Basic ' +
btoa(username + ":" + password));
}
get(query: string = '') {
let headers = new Headers();
this.createAuthorizationHeader(headers);
headers.append('Content-Type', 'application/json');
return this.http.get(this.API_URL + query, {
headers: headers});
}
getCategories() {
this.http.get('categories').subscribe((data) => {
this.Categories = data;
});
I constantly get Argument of type '{ headers: Headers; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
but when I use “post” instead of get it’s ok.
Am I doing something wrong ?
THank you
Ok so I updated my code to this, now I get "cannot read property ‘code’ from undefined
createAuthorizationHeader(headers: Headers) {
let username = 'test';
let password = "test";
headers.append('Authorization', 'Basic ' +
btoa(username + ':' + password));
}
get(query: string = '') {
let headers = new Headers();
this.createAuthorizationHeader(headers);
return this.http.get(this.API_URL + query, { headers: headers });
}
I guarantee that that error is not coming from anything you have posted, because I don’t see the word “code” anywhere.
1 Like
Yes you’re right, but there is no “code” in my code at all…
get(query: string = '') {
let username = "test";
let password = "test";
const httpOptions = {
headers: new HttpHeaders({
'Authorization': 'Basic ' +
btoa(username + ':' + password)
})
};
getCategories() {
this.http.get('categories')
.subscribe((data) => {
this.Categories = data;
});
}
As I’ve followed Angular official doc on headers, it gives me an other error. I assume it’s a CORS issue but I’m not sure, here is what Chrome outputs in console
Any help ?
I’ve found out why, you have to allow OPTIONS headers in Apache.
did you get solution? i also face same
HOw to allow OPTIONS headers in Apache ?