0

I am using angular 2 HTTP service call in my application. In this call I am using URL of my colleague's machine on which tomcat is running. This API call requires some headers as credentials, these headers also i have added in my code. Same API is working fine in Postman with same headers but while calling API from angular code its not working. Earlier it was showing error for CORS, to resolve it I used browsers CORS extension,now its showing me 403 i.e. access forbidden.

Here is my code:

import section:

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

constructor:

 constructor(private http: Http, private deviceService: Ng2DeviceService, private options: RequestOptions) { }

API call in onInit()

 ngOnInit() {
   let headerObj: RequestOptionsArgs = {
        method: RequestMethod.Get,
        url: "http://PC-NAME:8091/gateway/brand/poc",
        headers: new Headers({
            "SM_USER": "USER-ID@domain.com",
            "SM_USERUID": "USER#ID"
        }
        ),
    };
    this.http.get("http://PC-NAME:8091/gateway/brand/poc", headerObj).subscribe(res => {
        console.log(res.json())
    })
}

I have mocked credentials in above code. I think there is mistake in code or somewhere I am missing something as in Postman URL and headers working fine API cannot be wrong. If anyone can help me, it will be really helpful. Thank you.


I have also tried the Angular 4 way of HttpClient still no use... problem still persisting as it is. Here is angular 4 code

 this.httpClient.get("http://infom-lap120:8091/gateway/brand/poc", {
        headers: new HttpHeaders({
            "Content-Type": "application/json",
            "SM_USER": "Sujay.Anjankar.consultant@nielsen.com",
            "REPORT_SERVICE_TENANT_ID": "coop",
            "SM_USERUID": "44f4c95c01dcb2ed99d30c30965072a0"
        })
    }).subscribe(
        // Successful responses call the first callback.
        data => { },
        // Errors will call this callback instead:
        err => {
            console.log('Something went wrong!');
        })

All necessary steps are exactly same to this doc : angular.io/guide/http#headers

sajal rajabhoj
  • 507
  • 4
  • 14
  • Try setting the headers this way: https://angular.io/guide/http#headers. Also the new HttpClient for angular is preferable if you are up to date as the other is deprecated – 0mpurdy Aug 02 '17 at 08:33
  • thanks @0mpurdy, but this is for angular 4, I want solution for angular 2 – sajal rajabhoj Aug 02 '17 at 09:14
  • @0mpurdy as per your suggestion I migrated on angular 4 but still issue is continous – sajal rajabhoj Aug 03 '17 at 05:57
  • CORS usually requires a server side change [see this question](https://stackoverflow.com/questions/34790051/how-to-create-cross-domain-request-angular-2). Also [this question](https://stackoverflow.com/questions/44528135/403-forbidden-error-for-angular-2-spring-mvc-but-working-in-postman) is for spring but it is showing the same error in Angular (the same should apply to any back end). I'm unfamiliar with the extension you are using, could you check your back end code for CORS support and let us know? – 0mpurdy Aug 03 '17 at 06:50
  • @0mpurdy I tried this also , I added annotation in java side but issue is same. – sajal rajabhoj Aug 03 '17 at 12:35

0 Answers0