Titanium Community Questions & Answer Archive

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

Problems sending JSON requests using POST

It appears as though the setRequestHeader gets ignored for a HTTPClient which is being used to POST.

I have a REST service which consumes and produces application/json. however it doesn't like the requests from Titanium because requests are always sent with content-type:x-www-form-urlencoded regardless of what you specify using setRequestHeader().

Is this a bug? Is there a possible workaround?

var xhr = Titanium.Network.createHTTPClient();

xhr.onreadystatechange = function() {

    try {
      if (this.readyState == 4) {
           var results = JSON.parse(this.responseText);

        }
    } catch(e) {
        Ti.API.debug(e.error);
    }
};
xhr.open("POST","http://myrestserviceurl");
xhr.setTimeout(10);
xhr.setRequestHeader("Content-Type","application/json");
xhr.send('{"value1":"x","value2":"y"}');
— asked April 2nd 2010 by nobody anonymous
  • android
  • httpclient
  • json
  • setrequestheader
  • x-www-form-urlencoded
0 Comments

7 Answers

  • I recently tested and fixed this bug (or so I thought..), which version of the mobile sdk is your project using? (you can check in the "Edit" tab of developer)

    — answered April 2nd 2010 by Marshall Culpepper
    permalink
    0 Comments
  • Thanks Marshall,

    I'm using Titanium 1.1.2 on Windows. With Android 1.6 sdk.

    — answered April 2nd 2010 by nobody anonymous
    permalink
    0 Comments
  • Hmm.. Try sending the JSON inline instead of as a raw string (this is how our current test works)

    — answered April 2nd 2010 by Marshall Culpepper
    permalink
    0 Comments
  • I'm sending my request as follows and it is now sending the POST as application/json (perhaps it was before and i was getting confused).
    However the message gets sent all garbled. Are you able to show me a working example of how to send json data?

    var xhr = Titanium.Network.createHTTPClient();
    
    xhr.onreadystatechange = function() {
    
        try {
          if (this.readyState == 4) {
               var results = JSON.parse(this.responseText);
    
            }
        } catch(e) {
            Ti.API.debug(e.error);
        }
    };
    xhr.open("POST","http://myrestserviceurl");
    xhr.setTimeout(10);
    xhr.setRequestHeader("Content-Type","application/json; charset=utf-8");
    xhr.send({"value1":"x", "value2":"y"});
    
    — answered April 3rd 2010 by nobody anonymous
    permalink
    0 Comments
  • We are having the same problem as identified above, but when we did as one of the answers above suggested it still did not work, our server still sees the request as a form and not a json post. It seems the content type is not getting set during a post. Any ideas?

    — answered May 13th 2010 by Tom Wilson
    permalink
    0 Comments
  • We had a bug like this in our app. The problem was we were sending an object instead of a string. The error message about incorrect content-type seems to be wrong. After changing our code to send a string instead of an obejct it worked fine.

    — answered November 30th 2010 by David Smith
    permalink
    0 Comments
  • you should give URL with local host or the server IP address followed by port number….I think it works

    — answered October 5th 2011 by LOKESH KV
    permalink
    0 Comments
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.