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.

3
  • https://username:[email protected]/ Commented Sep 7, 2019 at 7:59
  • @joyBlanks what do you mean? Commented Sep 7, 2019 at 8:05
  • This is how a basic auth url looks like Commented Sep 7, 2019 at 8:06

2 Answers 2

0

Try like this:

const headers = new Headers({
  'username': "*****,
  'password': "*****,
})
return this.http.get('****', { headers: headers })
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.