0

I have an App in which I make requests like the one you see in the following code:

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ResourcesApiProvider {
  public direccion = my_url_app;

  constructor(public http: Http) {
  }

  getCars(car_id) {
    let repos = this.http.get(this.direccion + 'api/cars.json' + '?car_id=');

    return repos;
  }
}

The API I have responds both via HTTP and HTTPS for now, but I want to change all the communication to HTTPS, so I was wondering... what I can do so that Angular / Ionic the app sends encrypted requests to the API?, is it enough to just use the HTTPS URL of my API when I do the assignment public direccion = my_url_app?

I'm asking because some answers from here say that I have to add / at the end, but I'm not sure if that's still the case, etc.

Greetings.

8
  • you need to use rxjs and call the apis from there Commented Sep 30, 2018 at 17:20
  • Can you elaborate? Put an answer with some code as an example? etc. Commented Sep 30, 2018 at 17:43
  • @OiciTrap is your client application hosted on https as well? Commented Sep 30, 2018 at 18:16
  • The link you gave is for angular 1.x. just replacing http with https in your API url should be enough if they are on the same port Commented Sep 30, 2018 at 18:53
  • @PankajParkar The client is just a mobile app Commented Sep 30, 2018 at 19:11

1 Answer 1

0
import { Injectable } from '@angular/core';

import { Model } from './Model.model';

import { Http, Headers, Response, RequestOptions } from '@angular/http';

import { Observable } from 'rxjs/Rx';

import { LoginService } from '../../core/auth/login/login.service';

import { environment } from '../../../environments/environment';

@Injectable()

export class Service {

    requestOptions: RequestOptions;

    constructor(private http: Http, loginService: LoginService){
        const headers = new Headers({'Authorization': loginService.getToken()});
        this.requestOptions = new RequestOptions({headers : headers});
    }

    getEventsType(): Observable<any[]> {
        return this.http.get(`${environment.apiUrl}/events`, this.requestOptions)
            .map( mapAny );
    }

}

function mapAny(response:Response): any[] {

    // The response of the API has a results
    // property with the actual results

    return response.json().data;

}

You must to change it

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Please refer this help page How do I write a good answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.