0

Angular 6: I looking to create an REST API require in GET with a basic authentication. I don't understand how should put my params (user + password).

import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class DataService {

  constructor(private http: HttpClient) { }

  getCountry(){
    const params = new HttpParams().set('username', '****').set('password', '*****');
    return this.http.get('****', { params })
  }
}

In this way my user + password are in the path of the request and at the console I have this error:

Access to XMLHttpRequest at '****?username=****&password=****' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

2 Answers2

0

Try like this:

const headers = new Headers({
  'username': "*****,
  'password': "*****,
})
return this.http.get('****', { headers: headers })
Adrita Sharma
  • 17,967
  • 8
  • 40
  • 61
0

that endpoint doesn't exit. You are trying to hit a url that doesn't exit since you are creating the link dynamically And if you have that endpoint exactly how the dynamic URL looks like you need to modify your backend and give access to CORS.

devpato
  • 3,994
  • 6
  • 29
  • 67