Venkat
Venkat

Reputation: 115

Trying to pull values with parameters in angular

I'm trying to pass datas from "http://169.38.82.132:94/GetFarmerInfo" to "http://localhost:4200/". In this url "http://169.38.82.132:94/GetFarmerInfo" am trying to pass parameters as "authorisation and instance name" so my config file is:

{
  "/api": {
    "target": "http://169.38.82.132:94",
    "secure": false,
    "Authorization": "bearer uRf0e8upueR4qTh_HSjzF9KiVxuXN8qK4UqHZez4LdX7DYZOsee4h0Fq1PupKGTFEIzbjq9gjiaVOtPeoayIh212nDyHOEilH2chHeXz4pXzAb8YLC4aJZ0WAEOmq0werPUeaS5c5frXo_P3iwXFtTezCxsYRkNnrXoLOfkHJsxRGW91m19EqzQgqcY5q6jQUQRZeXB9KYpI6THLLnED7ZQDNJZgTjdcuKCb-Gmb1XHD_L_V_BDlbuaEcjH3Qx52f2wOsmtyWGGQEjRMfOFtpO3KJFFmpEIVH0WxsRl5L-eg7S21i_tuT1rDQMdapXiS",
    "InstanceName":"ORISSA",
    "pathRewrite": {
      "/": ""
    }
  },
  "logLevel": "debug"

}

and my service file is:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class PeopleService {

  constructor(private http:HttpClient) { }

  fetchPeople(): Observable<Object>{
    
    return this.http.get('/GetFarmerInfo');
    
  };

}

After run npm start am getting response as "GET http://localhost:4200/GetFarmerInfo 404 (Not Found)". Is there any other way to pass parameters?

Upvotes: -1

Views: 52

Answers (1)

Ali Wahab
Ali Wahab

Reputation: 592

Change your fetch people function to.

fetchPeople(): Observable<Object>{

    return this.http.get('http://169.38.82.132:94/GetFarmerInfo');

  };

You have to give the exact URL of your API. http://169.38.82.132:94/GetFarmerInfo in your HTTP function.

ADDING HEADERS

let headers = new HttpHeaders({
    'Authorization': 'bearer uRf0e8upueR4qTh_HSjzF9KiVxuXN8qK4UqHZez4LdX7DYZOsee4h0Fq1PupKGTFEIzbjq9gjiaVOtPeoayIh212nDyHOEilH2chHeXz4pXzAb8YLC4aJZ0WAEOmq0werPUeaS5c5frXo_P3iwXFtTezCxsYRkNnrXoLOfkHJsxRGW91m19EqzQgqcY5q6jQUQRZeXB9KYpI6THLLnED7ZQDNJZgTjdcuKCb-Gmb1XHD_L_V_BDlbuaEcjH3Qx52f2wOsmtyWGGQEjRMfOFtpO3KJFFmpEIVH0WxsRl5L-eg7S21i_tuT1rDQMdapXiS',
    'InstanceName': 'ORISSA'
});

Then pass headers in your function.

fetchPeople(): Observable<Object>{

    return this.http.get('http://169.38.82.132:94/GetFarmerInfo', { headers: this.headers});

  };

I would recommend you to use HTTP Interceptors for all your http request.

Try this code

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable({ providedIn: 'root' })
export class PeopleService {
  constructor(private http: HttpClient) { }
   headers = new HttpHeaders({ 'Authorization': 'bearer uRf0e8upueR4qTh....', 'InstanceName': 'ORISSA' });
  fetchPeople() { return this.http.get('http://169.38.82.132:94/GetFarmerInfo', { headers: this.headers }); };
}

Upvotes: 0

Related Questions