Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

HTTPClient will not send a POST HTTP request

All requests coming to the server from HTTPClient are GET requests, no matter what I set it to.

Here is the code:

var request = Titanium.Network.createHTTPClient(),
request.open("POST", "http://test.com");
request.onload = function() {
    try {
        var content = JSON.parse(this.responseText);
        if(content.error == undefined) {
            // do some things
        } else {
            alert(content.error);
        }
    } catch (err) {
        Titanium.API.error(err);
        Titanium.UI.createAlertDialog({
            message : err,
            title : "Remote Server Error"
        });
    }
};
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send({ test: 'test'});

I've also tried not setting the header and also passing in the data as a string instead of an object. No matter what the server says it is a GET request. Any help would be great. Thanks.

— asked July 25th 2011 by Keith Miller
  • httpclient
  • method
  • post
1 Comment
  • Sorry,

    var request = Titanium.Network.createHTTPClient(),
    

    should be:

    var request = Titanium.Network.createHTTPClient();
    
    — commented July 25th 2011 by Keith Miller

1 Answer

  • The order that the methods are set also is important.

    Please find below the KitchenSink example that shows when the open is issued.
    https://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/xhr_fileupload.js

    You might also want to add the onerror handler, it is useful in dealing with xhr errors

    — answered July 25th 2011 by Ben Bahrenburg
    permalink
    6 Comments
    • I'm using something like this

      var request = Titanium.Network.createHTTPClient();

      var done=false;

      request.onload = function() {

      try {
      
      if (this.readyState == 4 && !done) {
             done=true;
        if(this.status===200){
              var content = JSON.parse(this.responseText);
            }else{
              alert('error code' + this.status);
            }
      }
      
      } catch (err) {
          Titanium.API.error(err);
          Titanium.UI.createAlertDialog({
              message : err,
              title : "Remote Server Error"
          });
      }
      

      };

      request.onerror = function(e) {

      Ti.API.info(e.error);

      };

      request.open("POST", "http://test.com");

      request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

      request.send({ test: 'test'});

      — commented July 25th 2011 by Ben Bahrenburg
    • It's not working for me, the server still sees it as a GET request. Thanks for the help though!

      BTW, what version of the SDK are you using? I'm using:

      Titanium Mobile SDK Version: 1.7.2
      Mobile SDK Timestamp: 07/21/11 09:36
      Mobile SDK Build Number: 97c3689
      Titanium Mobile SDK Location: /Library/Application Support/Titanium/mobilesdk/osx/1.7.2

      — commented July 25th 2011 by Keith Miller
    • I'm using either the CI builds 1.8 or 1.7.1 both allow me to POST. Have you tried removing the requestheader?

      — commented July 25th 2011 by Ben Bahrenburg
    • I have. I even created a new app with just this code. No dice. Something must be wrong with my Titanium Studio, since this seems to work for everyone else.

      — commented July 25th 2011 by Keith Miller
    • Could it be your web server seeing the POST incorrectly? I've only tested against ASP.NET MVC.

      Another option might be to use a webview and see if that posts correctly. This would allow you to isolate if it is Ti or the server.

      — commented July 25th 2011 by Ben Bahrenburg
    • The following works:

      var request = Titanium.Network.createHTTPClient();
      
      request.onerror = function(e)
      {
          Ti.UI.createAlertDialog({title:'Error', message:e.error}).show();
          Ti.API.info('IN ERROR ' + e.error);
      };
      request.setTimeout(20000);
      request.onload = function(e)
      {
          var content = JSON.parse(this.responseText);
          Titanium.API.info(content);
          if(content.error == undefined) {
              // do some things
              alert('worked');
          } else {
              alert(content.error);
          }
      };
      request.onsendstream = function(e)
      {
          //ind.value = e.progress ;
          Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress);
      };
      request.open('POST','http://test.local');
      
      request.send({test:'test',purpose:'newaccount'});
      

      So weird. At least it's working now. Thanks for the help!

      — commented July 25th 2011 by Keith Miller
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.