cordova-plugin-advanced-http: Post Request Never Executes, No Response Returned

I posted this issue on Ionic Forum, but since it’s likely a plugin issue thought I’d post it here as well.

I’m using Ionic Native HTTP plugin 4.3.2 with Ionic Angular 3.7.1 on iOS 11 to POST data to a server. However the request never seems to get executed, it never gets back a HTTPResponse or even an error.

let url = "https://myserver.com";
let params = { name: 'Me' };
let headers = { 'Accept': 'application/json;charset=UTF-8' };
this.http.post(url, params, headers)
  .then((response:HTTPResponse) => {
    console.log(`Response ${response.data}`);
  })
  .catch((error:any) => {
    console.error(`Error ${error}`);
  });

Doing a GET request works fine returns back a HTTPResponse, just POST that doesn’t seem to work.

I tried to POST to different servers to rule out whether it was a server side issue. Did something break in a recent update of the plugin?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

@AlexGoo looks like that did the trick! Setting Content-Type helped, although also needed to change Accept from application/json;charset=UTF-8 to just application/json`.

Perhaps the application/json;charset=UTF-8 was causing the issue?

let url = "https://myserver.com";
let params = { "user": { "name": "Dale" } };
let headers = { };
this.http.setDataSerializer("json");
this.http.setHeader("Accept", "application/json");
this.http.setHeader("Content-Type", "application/json");
this.http.post(url, params, headers)
  .then((response:HTTPResponse) => {
    console.log(`POST ${url} ${JSON.stringify(response.data)}`);
  })
  .catch((error:any) => {
    console.error(`POST ${url} ${error.error}`);
  });

No worries @madsongr, I ran into the same problem. If you wanted to access the error in the catch, you’ll also need to convert it to JSON:

this.http.post(url, params, headers)
.then((resp:HTTPResponse) => {
    let json = JSON.parse(resp.data);
    console.log(json[0].USERNAME);
})
.catch((error:any) => {
   let json = JSON.parse(error.error);
   console.error(json);
});

I believe the resp.data is a string, so needs to be converted to JSON to access the response values.

let json = JSON.parse(resp.data); console.log(json[0].USERNAME);

@silkimen I’ve upgraded to "@ionic-native/http": "4.3.3" and "cordova-plugin-advanced-http": "1.7.1" but still no luck, POST never gets back a response.

Is there anything that could prevent the request from being executed? Checked the server logs and doesn’t look like it received the request.