Titanium for iOS: POST a JSON Object to a Web Service

Assume I’m using the boilerplate Ti.Network.createHTTPClient call with an “onload” & “onerror”.

    var xhr = Ti.Network.createHTTPClient({ ... });    
    myObj = { mydata:  
        { myitems: [ 
                { mainID:7452, someOption:87, theDate:'2012-10-23' },
                { mainID:2342, someOption:27, theDate:'2011-06-03' },
                { mainID:1914, someOption:43, theDate:'2012-02-14' }
    var strMyObj = JSON.stringify(myObj);
	Ti.API.info("strMyObj = " + strMyObj);

    xhr.open("POST", "http://www.yourPathHere.com/yourWebServiceName");
	xhr.send( strMyObj ); 

Works in CURL, not in Titanium

I ran into this issue first. The following CURL command worked in Terminal:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d "{mydata:{myitems:[{mainID:7452, someOption:87, theDate:'2012-10-23'}]}}"

In Titanium I kept getting HTTP response status “403” because I made stupid mistake, using “PUT” instead of “POST” in xhr.open(). Good times.
The “-i” stands for “include”, “-H” specifies an HTTP header. The “-X” in front of “POST” tells curl to use something other than “GET”. “-d” stands for data to include in your HTTP POST, mimicking an HTML form request. Here’s a list of CURL flags.


After fixing that, I got HTTP response status “500”, by just doing “xhr.send(myObj);”.

Thanks to some debugging by my backend dev Wes, it turned out the service simply wanted the data formatted as a String. He tried, taking the quotes off the {mydata…} line in the CURL command and got the same status 500 error in Terminal and via Chrome’s console:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d {mydata:{myitems:[{mainID:7452, someOption:87, theDate:'2012-10-23'}]}} 

curl: (6) Could not resolve host: someOption:87,; nodename nor servname provided, or not known
curl: (3) [globbing] unmatched close brace/bracket at pos 19
HTTP/1.1 500 Internal Server Error
Date: Fri, 05 Dec 2012 18:58:48 GMT
jsonerror: true
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 1761
{"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. (31): {mydata:{entries:[{mainID:7452,","StackTrace":"   at..."}

One thought on “Titanium for iOS: POST a JSON Object to a Web Service

  1. var uname=tname.value;
    var pass=tsurname.value;

    var jsonobj= { “username ” : ‘ ” ‘+uname+ ‘ ” ‘, “password” : ‘ ” ‘+pass+’ ” ‘ };

    var client= Ti.Network.createHTTPClient({

    client.setRequestHeader(‘content-type’, ‘JSON’);

    i want to enter the user define value into the jsonobject. i have tried the above code to the the value and send it over the web services. but when i am doing this blank values are getting tranfered. can help and tell me how can i do this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s