Dynamic URLS


#1

Hello, I am looking for a way to externalize port and host values of REST Urls that my app is consuming, any suggestions?


#2

Yes, change line 25 of your code to…

You should show us how you do it right now by posting some parts of your code.


#3

i have changed it, it did not work …LOL
my code is like :

@Injectable()
export class UtilsService {
	//private oauthUrl = 'http://41.142.250.29:8080/backendPay/oauth/token?grant_type=password&scope=read&client_id=restapp&client_secret=restapp';
	private oauthUrl = 'http://localhost:8084/backendPay/oauth/token?grant_type=password&scope=read&client_id=restapp&client_secret=restapp';
  constructor(public http: Http,public requestOptions:RequestOptions) {
     }
 
	 public  processPost (url:string,body:any): Observable<any> {
     let headers      = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
     let options       = new RequestOptions({ headers: headers }); // Create a request option
        return this.http.post(url, body, options) // ...using post request
                         .map(res =>{
						 return res.json()}) // ...and calling .json() on the response to return data
                         .catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...err
    }

i want to externalize URL to be definded as a global property.


#4

Guys, any suggestions here? Anybody? I need to solve the same problem …


#5

Well I think the return in the map function is needed. I would try writing that like this.

public  processPost (url:string,body:any): Observable<any> {
     let headers      = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
     let options       = new R`Preformatted text`equestOptions({ headers: headers }); // Create a request option
        return this.http.post(url, body, options) // ...using post request
                         .map(res => res.json()) // ...and calling .json() on the response to return data
                         .catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...err
    }

Or maybe this

public  processPost (url:string,body:any): Observable<any> {
     let headers      = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
     let options       = new RequestOptions({ headers: headers }); // Create a request option
        let post = this.http.post(url, body, options) // ...using post request
                         .map(res =>res.json()) // ...and calling .json() on the response to return data
                         post.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...err
return post
    }

#6

I would suggest reading about InjectionToken, starting with this thoughtram piece.