[Building Sakai] A note about POSTing data to EntityBroker

Steve Swinsburg steve.swinsburg at gmail.com
Thu Mar 22 17:00:05 PDT 2012


Hi all,

I've just spent a considerable amount of time battling through an issue I had with POSTing data to EntityBroker, in order to create entities.

To cut a long story short, you need to ensure the content type is being set correctly otherwise you will get unexpected results like the object being created but no data being populated.

 In the case of JSON, it must be:
application/json; charset=utf-8

cheers,
Steve


Read on for the longer story:

I have a simple HTML page I use, with a text area that accepts some JSON data, and then posts it to an EB endpoint via jQuery. I was also using cURL on the command line and was having the same problem.

Everything looked like it was working fine, and the object was being created from the JSON data in the createEntity method, however all of the fields in the object were null. Dumping out the object I'd get something like:

org.sakaiproject.testapp.tool.entityproviders.simple.SimpleObject at 5fa6dd41[title=<null>,description=<null>,location=<null>,category=<null>]

I worked through a number of scenarios with Aaron, intercepting the submitted data and checking it, and also checking the actual HTTP request that was being sent.

Web Inspector in Safari told me it was sending the data as:

Content-Type: application/x-www-form-urlencoded

I thought that was odd so adjusted it in my jQuery setup:

$.ajaxSetup({ 
        scriptCharset: "utf-8" , 
        contentType: "application/json; charset=utf-8"
});

Now it is setting it correctly and the object is being populated correctly:

Content-Type: application/json; charset=utf-8

org.sakaiproject.testapp.tool.entityproviders.simple.SimpleObject at 744f3123[title=title here,description=blah blah blah,location=somewhere,category=some cat]


cheers again,
Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://collab.sakaiproject.org/pipermail/sakai-dev/attachments/20120323/22cf2d2d/attachment.html 


More information about the sakai-dev mailing list