network.request params not going through to web service

I’m trying to register users and authenticate against a web service using the json and network libraries.

I have tested the web service endpoint with CURL and it is working correctly.  When I try to make the same request in Corona the body/params is not getting sent to the web service.

This is what my request setup looks like currently.

 local headers = {} headers["Content-Type"] = "application/json" local params = {} params.headers = headers -- Tell network.request() to get the request body from a file: params.body = json.encode({ ["username"] = "theuser", ["password"] = "apassword" }) network.request( "http://localhost:4567/register", "POST", registerListener, params )

That’s not normally how POST parameters work.  It still wants HTTP style key=value pairs separated by &'s

username=theuser&password=apassword

But if your script is looking for JSON data, you might have something like:

data=json.encode(…)

But without knowing what’s going on with your script, it’s hard to know exactly what you’re supposed to be sending.  Perhaps it would be beneficial to put some logging statements in your sever script if you have access to it to see what it’s getting and make sure the POST values are being set as expected.

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)

That’s not normally how POST parameters work.  It still wants HTTP style key=value pairs separated by &'s

username=theuser&password=apassword

But if your script is looking for JSON data, you might have something like:

data=json.encode(…)

But without knowing what’s going on with your script, it’s hard to know exactly what you’re supposed to be sending.  Perhaps it would be beneficial to put some logging statements in your sever script if you have access to it to see what it’s getting and make sure the POST values are being set as expected.

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)