3

I am working in the latest beta release of Ionic and I have done a http post method to my api server. But the headers are not being sent along with the request. The code that i have used is as below : ** 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");
                }
            );

When I look at console.log both options object and headers, the headers are set properly. But when I make the http request both the headers and body is not being sent when I enclose them in the options object. But when I try to send the body alone I am able to see it in the request payload.

1
  • 1
    Looks like you've forgot to pass body argument to the post() Commented Jul 11, 2016 at 8:40

3 Answers 3

3

That's what should work for you, since the second parameter for http.post is the body:

headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer ' + "tokenContent");
let options = new RequestOptions({ headers: headers });

this.http.post('http://myserver/myapi', body, options)
    .map(...
Sign up to request clarification or add additional context in comments.

3 Comments

I tried as you said but it is timing out on OPTIONS request and the headers are still not being sent..
Then you have an additional problem related to CORS: blog.ionic.io/handling-cors-issues-in-ionic
Definitely, this is the solution!
2

It's not working if you test from browser.. Please check the documentation related CORS requests.

http://blog.ionic.io/handling-cors-issues-in-ionic/

2 Comments

I have handled CORS issue in the backend. My problem is with the headers not being sent. I am making call to an authenticated route which requires the authorization token but as it is not being sent, I am not able to complete the transaction..
I had a similar issue. It's definitely a CORS issue.. If you comment the authorization append in the header you will receive a 401 exception. Please try to run code from an emulator (e.g. genymotion)
1

First, check if this problem is for CORS or not.

Also, try the code below:

let headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
this.http.post(`${this.link}`,'sRequest=' + sRequest1,{ headers: headers });

1 Comment

The problem was a CORS issue. It is solved now. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.