Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Request Never Executes, No Response Returned #41

Closed
dalezak opened this issue Nov 5, 2017 · 17 comments
Closed

Post Request Never Executes, No Response Returned #41

dalezak opened this issue Nov 5, 2017 · 17 comments

Comments

@dalezak
Copy link

dalezak commented Nov 5, 2017

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?

@silkimen
Copy link
Owner

silkimen commented Nov 7, 2017

Hi dalezak, I'm not able to reproduce your problem. Are you sure it's a plugin problem? Would you please try updating Ionic Native HTTP plugin to version 4.3.3?

@AlexGoo
Copy link

AlexGoo commented Nov 7, 2017

Hi, I have exactly the same problem. GET is working and POST no. Using http angular for the same post works.

@dalezak
Copy link
Author

dalezak commented Nov 8, 2017

@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.

@silkimen
Copy link
Owner

silkimen commented Nov 8, 2017

That's interesting. Can you please provide some more info?

  • Which OS is affected? iOS, Android, both?
  • Which OS version are you running on?
  • Is your application running behind a proxy, firewall, etc.?

@AlexGoo
Copy link

AlexGoo commented Nov 8, 2017

Hi,
In my case the problem appears only on ios. I have the last ios version 11.1.
Also I have updated to 4.3.3 and 1.7.1.

@silkimen
Copy link
Owner

silkimen commented Nov 8, 2017

Ok, I've created a ionic demo application with following components:

  • cordova-plugin-advanced-http 1.8.1
  • ionic-native/http 4.3.3

And this is my ionic setup:

cli packages: (/Users/sefa/.nvm/versions/node/v7.4.0/lib/node_modules)

    @ionic/cli-utils  : 1.9.2
    ionic (Ionic CLI) : 3.9.2

global packages:


    Cordova CLI : 7.0.1

local packages:

    @ionic/app-scripts : 2.1.4
    Cordova Platforms  : android 6.2.3 ios 4.4.0
    Ionic Framework    : ionic-angular 3.6.0

System:

    ios-deploy : 1.9.1
    ios-sim    : 6.0.0
    Node       : v7.4.0
    npm        : 4.0.5
    OS         : macOS Sierra
    Xcode      : Xcode 9.1 Build version 9B55

I'm using following URL for testing purposes "http://httpbin.org/anything". I've tried setting data serializer to "json" and "urlencoded".

I've tested it on iPhone X emulator with iOS 11.1 and on a real device iPhone 6s with iOS 11.0.3. I can't reproduce this problem.

Maybe you could try debugging the plugin native code on your machine to find out whats wrong?

@AlexGoo
Copy link

AlexGoo commented Nov 10, 2017

Hi,

So I have solved the issue by updating to 1.8.1 and before request setting the encoding to json or urlencode, depending on what I need. Also one thing that I saw and I did not find anywhere to be specified is that when using POST or PUT the param sent as payload must be passed as an object not as JSON stringfied or an already encoded url because the plugin will attempt to stringfied what you sent as param so will be doubled stringfied as json or url encoded.

@dalezak
Copy link
Author

dalezak commented Nov 10, 2017

Thank you @AlexGoo, I'll test it out!

Looks like 1.8.1 has the function setDataSerializer:

Set the data serializer which will be used for all future POST and PUT requests. 
Takes a string representing the name of the serializer. 
The name of the serializer. Can be urlencoded or json.

http://ionicframework.com/docs/native/http/#setDataSerializer

@dalezak
Copy link
Author

dalezak commented Nov 10, 2017

Hmm @AlexGoo, I upgraded to 1.8.1 but still no luck 😢

Here's a sample of what my POST looks like:

let url = "https://myserver.com";
let params = { "user": { "name": "Dale" } };
let headers = { };
this.http.setDataSerializer("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}`);
  });

I also removed the /plugins directory, then removed and re-added the iOS platform to make sure the plugin was upgraded.

@AlexGoo
Copy link

AlexGoo commented Nov 10, 2017

I think you also need to add the content type as header. Also did you intercepted the post to be sure that the body is ok formatted ?

@dalezak
Copy link
Author

dalezak commented Nov 10, 2017

@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}`);
  });

@dalezak
Copy link
Author

dalezak commented Nov 13, 2017

I believe this is now resolved, closing issue 👍

@dalezak dalezak closed this as completed Nov 13, 2017
@madsongr
Copy link

Why am I not able to get the values from response?

signup() {
    this.http.setDataSerializer('json');
    this.http.post(this.url, this.todo, this.headers)
    .then(res => {
      console.log(res.data);
      console.log(res.data[0].USERNAME);
    }).catch(error => {
      console.log(error);
    });
  }

console.log(res.data);
[{"ID":"15","USERNAME":"user01","EMAIL":"user01@mail.com"}]

but console.log(res.data[0].USERNAME);
undefined

@dalezak
Copy link
Author

dalezak commented Apr 1, 2018 via email

@madsongr
Copy link

madsongr commented Apr 2, 2018

Thanks man! I was using res.data.toJSON()[0].USERNAME but it was returning an errror.

@dalezak
Copy link
Author

dalezak commented Apr 2, 2018

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);
});

@jsasmitha
Copy link

Hi, I ran into the same problem. POST is not sending data to the server. GET is working fine. Below is my code:-
post(user){
this.http.setDataSerializer("json");
let bodynow = {
"email_id": "aa@gmsd.sdf",
"mobile":"9978897989",
"first_name": "dfdf",
"last_name": "ghgfh",
"user_title":"MS",
"country_code": "91"
}
let headers=this.http.setHeader('',"Accept", "application/json");
headers=this.http.setHeader('
',"Content-Type", "application/json");
const url =
"https://www.allevoyage.com/index.php/hotels_android/update_profile";
this.http.setDataSerializer("json");
this.http
.post(url, bodynow,headers)
.then(data => {
console.log(data,data.url);
})
.catch(err => {
console.log(err);
});
}
cordova-plugin-advanced-http version:-2.0.1
ionic-native/http version:-4.17.0
"cordova-ios": "4.5.5"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants