Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 make http use https

Tags:

angular

So, my app is being served by a simple web server, under "https://localhost:8443/app", while the same web server returns json responses under eg. "https://localhost:8443/api/login".

TLS is set up with a self signed certificate for now.

when I request the app, I get the certificate warning, which I skip, and then I see my login page.

on clicking the login button, the angular application executes

http.post<LoginResponse>('/api/login', body)

which shows up in my browsers network trace as a post request to "http://localhost:8443/api/login".

I cannot find any info on this, only infos on how to handle tls when running the app with angular-cli.

Why does the app default to http, and where do I tell it to use https?

like image 593
billdoor Avatar asked Nov 01 '18 15:11

billdoor


People also ask

Which option do you pass to Ng serve to use https?

SSL key to use for serving HTTPS.

What is the difference between HttpClient and HTTP in Angular?

The HttpClient is used to perform HTTP requests and it imported form @angular/common/http. The HttpClient is more modern and easy to use the alternative of HTTP. HttpClient is an improved replacement for Http. They expect to deprecate Http in Angular 5 and remove it in a later version.

Does Angular HttpClient use Fetch?

Angular offers HttpClient to work on API and handle data easily. In this approach HttpClient along with subscribe() method will be used for fetching data.


1 Answers

In the Angular documentation, they have an example where they use an interceptor to ensure https. The code snippet below is taken from their documentation:

// clone request and replace 'http://' with 'https://' at the same time
const secureReq = req.clone({
url: req.url.replace('http://', 'https://')
});
// send the cloned, "secure" request to the next handler.
return next.handle(secureReq);
like image 51
dandashino Avatar answered Oct 20 '22 22:10

dandashino