Angular 2 HTTPS Request

How to configure and make https request from angular 2? Could not find any resources. Can you someone help me here? Thank.

+4
source share
5 answers

So just 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))
}
} 
+1
source

short answer, add this on top of page.ts page:

import {Http} from '@angular/http';

then inside 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 an http request function as follows:

httprequest(url) {
        this.http.request(url).map(res => JSON.parse(res['_body']));
        }

then use it anywhere in the class of your page as follows:

this.httprequest('http://your_request_url').subscribe(yourdata=>{
console.log('you have your data: '+JSON.stringify(yourdata));
});

* JSON.parse, JSON. res ['_ body'], .

0

, URL- , , - https.

, , https, I.E. 'https://request.url.com'.

, "/api/some/data", https.

0

, , , .

angular http interceptor , angular docs

0

, http-

import {Injectable} from '@angular/core';
import {Http, URLSearchParams} from '@angular/http';
import { Observable }     from 'rxjs/Observable';

@Injectable()
export class WikipediaService {
  constructor(private http: Http) {
  }

  search(term: string) {
    let wikiUrl = 'http://en.wikipedia.org/w/api.php';
    let params = new URLSearchParams();
    params.set('search', term); // the user search value
    params.set('action', 'opensearch');
    params.set('format', 'json');
    params.set('callback', 'JSONP_CALLBACK');
    // TODO: Add error handling
    return this.http
      .get(wikiUrl, {search: params})
      .map(response => <string[]> response.json()[1])
      .catch(this.errorHandler);
  }

  private errorHandler(error: any) {
    let errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server Error';
    console.log(errMsg);
    return Observable.throw(errMsg);
  }
}

, search(), .

  • Http '@angular/http'.
  • Http , constructor(private http: Http)
  • than use this line of code to make a request GET return this.Http .get(wikiUrl, {search: params}) .map(response => <string[]> response.json()[1]) .catch(this.errorHandler);
-2
source

Source: https://habr.com/ru/post/1655895/


All Articles