From the corona documentation listed here http://docs.coronalabs.com/api/library/network/request.html this is how corona is handling posting a file as a JSON object.
-- The following code demonstrates an HTTP post, where the request body is text from a file. -- This example demonstrates just one possible way to upload a file. Different web servers -- and applications support different methods of file upload. For example, some REST services -- support upload of a binary file in the body of a PUT request. Many web servers only allow -- file uploads using multipart/form-encoded (text) request bodies in a POST request. If you -- are going to attempt file upload, you must first understand the specific mechanism -- supported by the specific web server or application that you will be working with, -- then you must form your request data and choose your request method appropriately. local function networkListener( event ) if ( event.isError ) then print( "Network error!" ) else print ( "Upload complete!" ) end end local headers = {} headers["Content-Type"] = "application/json" headers["X-API-Key"] = "13b6ac91a2" local params = {} params.headers = headers -- Tell network.request() to get the request body from a file: params.body = { filename = "object.json", baseDirectory = system.DocumentsDirectory } network.request( "http://127.0.0.1/restapi.php", "POST", networkListener, params )
Posting a serialized JSON object should work similarly. When I tested this from CURL it worked fine with my web service. When I POST with the snippet I showed originally on the web service the params does not show up in the request at all when I debug from the server side.
I did end up working around this by base 64 encoding my JSON and then stuffing it into a URL parameter. For now that works, but if the data is large enough then that becomes a problem. Currently I’m working on user stats and authentication so the dataset is small. I could see this becoming a problem in some situations though. Possibly writing to a file then sending the file similar to posted in the doc would work, although this seems like an unneccessary hack.
The following is what I ended up using in this case.
body = json.encode({ ["email"] = "emailaddy@foobar.com", ["username"] = "theuser", ["password"] = "apassword78", ["confirm\_password"] = "apassword78" }) data = mime.b64(body) network.request( SERVER .. "register/" .. data, "POST", userRegistrationListener)