Titanium & Usergrid

Posted by Quinn Madson | Posted in | Posted on 7:39 PM

0

I was trying to get a Titanium app up and running with a Usergrid backend and I kept getting the error:

[INFO] {"error":"web_application","timestamp":1344133939838,"duration":0,"exception":"javax.ws.rs.WebApplicationException"}

Turns out it was an issue with how the the Titanium HTTPClient serializes JSON data. (I thought the Ti API did this auto-magically but, maybe I'm mistaken.) If you run into the same problem, call JSON.stringify() manually on the JSON data that you are posting. Seems to do the trick. Here is an example:

var xhr = Ti.Network.createHTTPClient();
            
xhr.open("POST", "https://api.usergrid.com/YOUR_ORG-NAME/YOUR-APP-NAME/users");
xhr.setRequestHeader('Content-Type','application/json');

xhr.onload = function(){
    try {
        Ti.API.info(this.responseText);                
    } catch(e) {
        Ti.API.info(e);
    }
};
    
var data = {
    username: "raoul.duke",
    email: "raoul.duke@doomed.net",
    name: "Raoul Duke",
    password: "b4tc0untry!!"
};
                
xhr.send(JSON.stringify(data)); //this is the important bit