I'm trying to hit an API to do a pretty simple get
request but the options doesn't seem to be getting supplied as I'm getting an authentication error, even though the same auth token works on Postman. Here's the code:
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable()
export class UsersService {
private nearby_url: string = APP_CONFIG.apiEndpoint + '/users/closest/';
private me_url: string = APP_CONFIG.apiEndpoint + '/users/me/';
private conversations_url: string = APP_CONFIG.apiEndpoint + '/users/conversations/';
constructor(private http: HttpClient) { }
createAuthorizationHeader() {
var headers = new HttpHeaders();
// get auth token
// append auth token to headers
headers.append("Authorization", 'Token xxxxxxxxxxxyyyyyyyyyyyyzzzzzzzzz');
return headers
}
getPersonResponse(url): Observable<PersonResponse> {
const options = {
headers: this.createAuthorizationHeader()
};
return this.http.get(url, options)
.map((response: Response) => response)
.catch((error: any) => Observable.throw(error.error || 'Server error'));
}
getNearby(): Observable<PersonResponse> {
return this.getPersonResponse(this.nearby_url);
}
getMe(): Observable<PersonResponse> {
return this.getPersonResponse(this.me_url);
}
getConversations(): Observable<PersonResponse> {
return this.getPersonResponse(this.conversations_url);
}
}
What am I doing wrong here?