I am trying to make the a http.get
request with a basic
auth where I have a token
as a username and then an empty
password.
const headers_object = new HttpHeaders();
headers_object.append('Content-Type', 'application/json');
headers_object.append('Authorization', 'Basic ' + btoa(environment.CVRTOKEN + ':'));
const httpOptions = {
headers: headers_object
};
const url = 'https://rest.cvrapi.dk/v1/dk/suggestions/company/' + searchString;
return this.http.get(url, httpOptions);
However it doesn't seem to work. What have I done wrong?
When looking at the console I get the following error:
VM3464:1 GET https://rest.cvrapi.dk/v1/dk/suggestions/company/3 401 (Unauthorized)
And the request headers look like this:
HttpHeaders
is an immutable type, which means that append
returns a new instance of HttpHeaders
rather than modifying the existing version. In your code example, you're not actually setting the Content-Type
and Authorization
headers. You can use the following to set the headers correctly:
const headers_object = new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(environment.CVRTOKEN + ':')
});