Reputation: 1085
How to configure and make a https request from angular 2?
Couldn't find any resources on it.
Can please someone guide me here?
Thank you.
Upvotes: 11
Views: 49081
Reputation: 8568
Just in case someone else is trying to do this
You should make the https calls just like the normal http calls but you have to ensure that your browser trust the certificate that is used by the backend APIs & that your browser can connect to this api.
Note:
It might give timeout error in YARC or REST clients so you should try a simple GET request directly in the browser (i.e: navigate to https get request)
For example, if we have this API
Try to make a get request to this url by navigating to it in the browser, if you are testing locally or in UAT, your certificate might not be fully trusted in the browser automatically, so you have to tell chrome that you trust this website & go there (proceed to this website)
If you can successfully call this get request in the browser:
Then you can call the https apis just as you call normal http (with full correct URL having the https & the port number) as per the below resources:
2- You can also check out this excellent guide on making http calls in Angular 5
Upvotes: 10
Reputation: 1270
This answer is a little bit late, but for the people just searching it, this might be helpful.
With the latest version of angular you can use http interceptor
to do this as is shown in the angular docs
Upvotes: 2
Reputation: 93
It's so simple try this:-
import { Http, Headers } from '@angular/Http';
export class service{
constructor(private http:Http){
let headers = new Headers({ 'Content-Type': 'application/json' });
this.http.post(requestUrl,headers,body)
.map(val => val.json())
.subscribe(data => console.log(data))
}
}
Upvotes: -1
Reputation: 767
Just make sure that the request URL you pass into the parameters is https.
If you're using a full path then that's easy, you can specify https, I.E. 'https://request.url.com'.
If it's a relative path to your application such as '/api/some/data' then your app should be hosted securely and reached using a https connection.
Upvotes: 2
Reputation: 123
short answer, add this on top of your page.ts:
import {Http} from '@angular/http';
then inside in your page class(below @component), in your constructor, add 'public http:Http' like this:
constructor(public http:Http, public navCtrl: NavController, public navParams: NavParams) {}
then outside your constructor(inside your page class), make http request function like this:
httprequest(url) {
this.http.request(url).map(res => JSON.parse(res['_body']));
}
then use it anywhere within your page class like this:
this.httprequest('http://your_request_url').subscribe(yourdata=>{
console.log('you have your data: '+JSON.stringify(yourdata));
});
*you don't need JSON.parse if it's not JSON data. just res['_body'] is where you get your data.
Upvotes: 0