I'm currently working with the latest beta release of Ionic and I've encountered an issue with sending headers along with an HTTP POST request to my API server. The code snippet I used is as follows: ** Ionic version - Beta-8 & Angular version -rc.3
import {Page,App,NavParams} from 'ionic-angular';
import {Headers, Http, RequestOptions} from '@angular/http';
import {Component} from '@angular/core';
import 'rxjs/add/operator/map';
@Component({
templateUrl : 'build/pages/xyz/xyz.html'
})
export class Xyz{
form:any;
token:any;
constructor(public app:App, navParams:NavParams, public http:Http){
let code = {abc : 'abc'};
let headers = new Headers();
let body = JSON.stringify(code);
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer ' + "tokenContent");
let options =new RequestOptions({headers : headers, body:body});
this.http.post('http://myserver/myapi', options)
.map(res => res.json())
.subscribe(
data=>{
console.log(data.message);
},
err=>{
console.log(err);
},
()=>{
console.log("Process Complete");
}
);
Upon checking the console.log, both the options object and headers are properly set. However, when I actually make the HTTP request, neither the headers nor the body are being sent when enclosed in the options object. Surprisingly, when I attempt to send the body alone, it does show up in the request payload.