ionic-native/http does not submit data on post

Created on 9 Aug 2017  Â·  7Comments  Â·  Source: ionic-team/ionic-native

I'm submitting a ... (check one with "x")
[x] bug report
[ ] feature request

Current behavior:
We are trying to use Ionic Native HTTP which use codrova-HTTP, to do a simple get works very well, something like that:

        this.http.get('https://blabla.blabla.com/login', {}, {})
                     .then((data) => {
                         console.log(data);
                     })
                     .catch((error) => {
                         console.log(error);
                     });

But we can not do a POST with data and Content-Type header set.

        let data = {
            'Action': 'Login',
            'UserName': 'bla',
            'Password': 'blabla'
        };
        let headers = {
            'Content-Type': 'application/json'
        };

        this.http.post('http://10.0.2.2:3000/cloudapi/login', data, headers)
                    .then((data) => {
                        console.log(data);
                    })
                    .catch((error) => {
                        console.log(error);
                    });

In this case the POST is send to node proxy but we log there header and body. But there is no body in the request and the header is not set with ‘application/json’.
We tried to do many things with data and header but nothing changed.

It seems the data would not be submitted to cordova plugin?

Most helpful comment

So what worked for me was:

this.http.setDataSerializer('json');
this.http.post('url here', {"title": "something"}, {"Content-Type": "application/json"})
    .then(data => console.log(data))
    .catch(error => console.log(error));

All 7 comments

Facing similar issues. Any solution or workarounds?

@naveddeshmukh Can you try to update cordova plugin and ionic native to 4.2.0 thy changed as suggested to another cordova plugin see PR #1921 and see new documentation: https://ionicframework.com/docs/native/http/

@mburger81 Things are working fine now for me at least. Time to close the issue?

So what worked for me was:

this.http.setDataSerializer('json');
this.http.post('url here', {"title": "something"}, {"Content-Type": "application/json"})
    .then(data => console.log(data))
    .catch(error => console.log(error));

The above solution which @jkiyomu has provided worked for me.

a question this works on iOS device ?

@armandozabala yes it does

Was this page helpful?
0 / 5 - 0 ratings