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.setRequestHeader('Accept','application/json'); 
	xhr.setRequestHeader('Content-Type','application/json'); 				
	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'}]}}"
 http://www.yourPathHere.com/yourWebServiceName

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.

JSON.stringify

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'}]}} 
 http://www.yourPathHere.com/yourWebServiceName

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
Server:...
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..."}

Unity3D for iOS and Android: UniWeb and UniParse plugins for HTTP

If you’ve ever had to work with a REST API from within a Unity project or use web services and back end data in general, you’re probably seen the limitation of Unity’s built in WWW class. Lucky for us, the developers at Different Methods released an awesome plugin that provides full HTTP functionality for Unity3D. It’s called UniWeb. UniParse is a free plugin, still in beta, that works with UniWeb and a backend service called Parse.

I owe a special thank you to Simon Wittber of Different Methods for super speedy and excellent customer service help with resolving a Unity Asset Store download issue.

Here’re a few deeper UniParse examples:

My buddy Steve, went through the following basic steps to get UniWeb up and running on an iPhone 4S and and several Android models. The Android phones included:

  • Motorola DroidX (Android 2.3.4)
  • Samsung Galaxy (Android 2.1)
  • Samsung Galaxy Nexus (Android 4.0.4, Ice Cream Sandwich)

Testing UniWeb

What is UniWeb? It’s an “HTTP glue” plugin for Unity3D. It allows for much more functionality than Unity’s built in WWW class. For example, Parse’s REST API sometimes requires the use of the PUT method instead of GET or POST. WWW only works with GET & POST. UniWeb handles PUT just fine. While I haven’t tried all of UniWeb’s functionality, it has classes to handle stuff like JSON parsing & Socket connections.

Here’s a basic test we did over HTTP:

  1. Installed the UniWeb .unitypacakge
  2. Opened up the basic UniWebExample scene that comes with UniWeb
  3. At first, we got a minor error telling us the socket connection failed because the socket server doesn’t exist. This was easily fixed – we removed the Socket script from the test GameObject.
  4. The UniWebExample image loading script worked perfectly
  5. Next, we ran into a cross compilation error when we tried to put the test app on an iPhone. Steve fixed this by switching the Api Compatibility Level from “.NET 2.0 Subset” to .”NET 2.0″ in Build Settings > Player Settings (under iOS platform settings, Other Settings > Optimization)
  6. Shortly after Steve sent an APK of this test app to several folks with Android devices and the app worked well for them. The images loaded & cycled through once, just like on the iPhone version.

Testing UniParse with UniWeb

In layman’s terms Parse is a back end service for mobile developers. It frees mobile developers up from having to code their own databases & middleware language logic to manage data between those databases & mobile applications. UniParse is Different Methods’ plugin for talking to Parse from inside a game or mobile app built with Unity3D.

The UniParse plugin uses an Update() method that does an HTTP ‘PUT’. There’s also Delete, which does HTTP ‘Delete’.

Testing UniWeb over HTTPS

coming soon…