How to use ionic native HTTP to perform a post with body and header

thx, it works fine!!

unable to find setDataSerializer method.
coming as undefined.

Dude. This was it. Thanks so much!

1 Like

Its kinda lame to ask but could you provide example for sending a data using post request to a mysql server, i cant find any example using ionic native http in sending data to a server. thanks

1 Like

this.http.setDataSerializer(‘http’);

THANK YOU

You have no idea how long it took me to find that missing piece of this extremely frustrating puzzle.

2 Likes

thanks alot for such support

I just changed as you said but for utf8 and it worked perfectly for application/x-www-form-urlencoded

thank you!

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));
3 Likes

this works, but the backend don’t receive any data…
my php prints the var_dump($_POST) as array(0) {}
any thoughts?

I was searching a lot about this. Your tip works great. I would like to draw ionic team attention those who make the documentation most of the docs are pretty basic. it dose not explain how some functions works, we could save a lot of time if what our friend has written was documented in the docs.

thanks for your contribution

1 Like

below code use to receive data
$postdata = json_decode(file_get_contents(“php://input”),true);

The fix still WORKS!! After 2 years. Thanks.

It worked for me, thanks a ton.

Hello, I tried your solution but it’s not working ! here’s my code :

this.http.setDataSerializer(‘json’);
this.http.post(‘https://mydev.appsfarm.net/rest/api/login’, { “loginName”: FormLogin.username ,"&password" : FormLogin.password,“output” : “json”} ,{“Content-Type”: “application/json”} )
.then(data => console.log(data))
.catch(error => console.log(error));

Please Help me

Remove & symbol in you r code
And check console log

Thank you a billion times. I didn’t realize it was this simple

Hi, I need help in resolving this issue. I’m facing issue when doing POST request to get the access token in IOS. Whereas it works fine in Android.

Code:
const bodyParams = {‘client_id’:clientId,‘client_secret’:secret,‘grant_type’:‘authorization_code’,‘code’:tokenOrCode};

const httpResponse = await this.http.post(accessTokenUrl, bodyParams, {“Content-Type”: “application/json”});

Error:
{“error”:“invalid_client”,“error_description”:“FBTOAU204E An invalid secret was provided for the client with identifier: ‘abc@abc.com’.”}

Note: The above code works without any issues in Android.

In 2020 its worked, Thank you!!!

1 Like

Thanks. This worked for me too. I think you can not use a json request body without data serializing by calling this.http.setDataSerializer(‘json’) method.

Thank you. It helps me very well. Still work in 2021.