I have a nodeJS service and Angular2 Application. when I call a get method by passing headers to NodeJS , the headers are not coming. Can anyone please tell me where I'm doing it wrong.
Note: When call the same service by passing header from Fiddler it is working fine. Only problem is from Angular application.
What I have tried:
package.json
{
"name": "SampleApp",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/angular/angular.io/blob/master/LICENSE"
}
],
"dependencies": {
"@angular/common": "~2.2.0",
"@angular/compiler": "~2.2.0",
"@angular/core": "~2.2.0",
"@angular/forms": "~2.2.0",
"@angular/http": "~2.2.0",
"@angular/platform-browser": "~2.2.0",
"@angular/platform-browser-dynamic": "~2.2.0",
"@angular/router": "~3.2.0",
"@angular/upgrade": "~2.2.0",
"angular-in-memory-web-api": "~0.1.15",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"@types/core-js": "^0.9.34",
"@types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
}
---------------------------
app.component.ts
import { Component } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import {Observer} from 'rxjs/observer';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Component({
selector: 'my-app',
templateUrl:'./app/app.component.html'
})
export class AppComponent {
firmID: string;
flag: string;
uKeys: string[];
uris: string[] = ["http://localhost:8080/api/asps","http://wd-dbn-boops004/api/asps"];
selectedUri: string;
hdrJson:Object;
options: RequestOptions;
constructor(private http: Http){
this.selectedUri = this.uris[0];
this.firmID="1";
this.flag="-1";
this.uKeys=["1405"];
}
OnQuery(){
let hdr = new Headers({'content-type': 'application/json','auth-ticket':'abcdd'});
console.log("selected Uri: "+this.selectedUri+"; firmID: " + this.firmID + "; Flag: " + this.flag + "; Ukeys: " + this.uKeys);
console.log("Calling service uri :"+this.selectedUri+"/"+this.firmID+"/"+this.uKeys);
console.log("headers :"+ hdr);
this.hdrJson = JSON.stringify(hdr);
console.log(this.http.get(this.selectedUri+"/"+this.firmID+"/"+this.uKeys,{headers:hdr})
.map((res: Response) => res.json())
.catch(this.handleError));
}
private handleError(error: any) {
console.error('An error occurred', error);
return error;
}
}