Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - Dynamically find base url to use in the http requests (services)

I'm wondering if there is a dynamic way of getting the base url, to use in the http requests?

Is there any way of getting the http://192.123.24.2:8080 dynamically?

public getAllTickets() {
    this._http.get('http://192.123.24.2:8080/services/', {
        method: 'GET',
        headers: new Headers([
            'Accept', 'application/json',
            'Content-Type', 'application/json'
        ])
    })

So, I my request would look something like:

public getAvailableVersions() {
    this._http.get('../services', {
        method: 'GET',
        headers: new Headers([
            'Accept', 'application/json',
            'Content-Type', 'application/json'
        ])
    })  

I'm looking for a way to not having to hard code the URL for the REST calls. Or is the only option to have a global variable with the URL?

Thanks!

like image 986
Sojye Avatar asked Sep 25 '22 08:09

Sojye


1 Answers

You can create a file with your credentials

credentials.ts

export var credentials = {
  client_id: 1234,
  client_secret: 'secret',
  host: 'http://192.123.24.2:8080'
}

And import it into your file

import {credentials} from 'credentials'

public getAllTickets() {
    this._http.get(credentials.host + '/services/', {
        method: 'GET',
        headers: new Headers([
            'Accept', 'application/json',
            'Content-Type', 'application/json'
        ])
    })

And with that you can handle dev/prod credentials

like image 110
Raphael Avatar answered Sep 28 '22 05:09

Raphael