Как я могу сделать базовую авторизацию с угловым HttpClient и подключиться с помощью API-запроса? - PullRequest
0 голосов
/ 27 апреля 2018

хорошо, у меня проблема, я пытался подключить API с базовой авторизацией, но сервер не дает мне доступа, он возвращает 401 (без авторизации) мой код:

getApi() {
        console.log('here i am in the method for get extensions');
        const headers = new HttpHeaders({
            'Content-Type': 'application/json',
            'Authorization': 'Basic ***********************'
        });
        const options = {
            headers,
            withCredentials: true
        };
        // tslint:disable-next-line:max-line-length
        return this.http.post('https://10.100.43.241/json', this.jsonBody, options).map((response: Response) => {
            const resToJSON = JSON.stringify(response);
            console.log('i am going to return jsonfrom method');
            return resToJSON;
        });
    }

Я тоже пытался с почтальоном, он тоже работает . Мне действительно нужно знать, как я могу решить эту проблему подключения или авторизации

примечание: я не являюсь администратором сервера

1 Ответ

0 голосов
/ 30 апреля 2018

Попробуйте эту архитектуру.

Компонент:

this._appApiService.getApi(this.jsonBody).subscribe(result => {
   this.resToJSON = result; 
});

Услуги:

getApi(jsonBody: any) {
    // add authorization header with jwt token
    let headers = new HttpHeaders({ 'Authorization': 'Bearer ' + this.token });
    let options = { headers: headers };

    return this.http.post(this.baseUrl + 'https://10.100.43.241/json', this.jsonBody , options);
  }
...