network.request post not sending data - what am I doing wrong?!

Hi All,

I have some code that saves a display object from the screen to the TemporaryDirectory - that’s working fine.

Reading the jpg back into the a variable - that’s working fine

useing mime.b64 to convert the above variable into a base64 variable - that’s working fine

Then I create a network request using the code below but it seems that the POST isn’t posting anything at all…any thoughts?

-- \_filename = specified above and is a valid filename. local \_file\_obj\_path = system.pathForFile(\_filename, system.TemporaryDirectory) local \_file\_obj = io.open(\_file\_obj\_path, rb) local \_file\_obj\_data = \_file\_obj:read("\*a") io.close(\_file\_obj) \_file\_obj=nil local \_file\_b64 = mime.b64(\_file\_obj\_data) local \_file\_obj\_size = string.len(\_file\_b64) -- post to CMS local p\_params = {} local headers = {} headers["Content-Type"] = "multipart/text" headers["Accept-Language"] = "en-US" p\_params.headers = headers p\_params.progress = "upload" p\_params.body = "hash="..string.urlEncode(hash).."&title="..string.urlEncode(text\_comments.text).."&user\_id=".. globalData.config\_table.user.id .. "&image\_name=" .. string.urlEncode(\_filename).."&image\_base64="..string.urlEncode(\_file\_b64) network.request("http://www.....com/index.php?user\_id=16", "POST", uploadListener, p\_params)

My Upload listener get the response from the PHP file, the $_GET[‘user_id’] is coming through fine but literally nothing in the $_POST at all.

Also tried multiple different Content-types and not including any headers - nothing makes a difference.

_file_obj_size is the size of the image( roughly of course as the base64 encoding adds a few characters here and there)

HELP! 

Many thanks

Chris.

I just tried, this works:

main.lua:

network.request("http://localhost/test.php","POST", function( e )     print(e.response) end, {     body = "message=hey", })

test.php

\<?php var\_dump($\_POST); ?\>

Prints out:

array(1) { &nbsp; &nbsp; ["message"]=\> &nbsp; &nbsp; string(3) "hey" }

There is a pretty well known issue with PHP and $_POST array’s being empty. It’s not a Corona issue, but a PHP and hosting issue.  If you Google “PHP $_POST empty”:

https://www.google.com/search?q=php+%24_post+empty&oq=php+%24_POST

You will get a bunch of hits about this.

Rob

Thanks for coming back Vlad - I’ve sorted it between then and now - turns out it was down to the url form encoding. I needed to change the Content-type to application/x-www-form-urlencoded

Agreed Rob (obviously).

Ended up using the PHP:

$sets = explode('&', file\_get\_contents('php://input')); for($i=0;$i\<count($sets); $i++) { $keyval = explode('=', $sets[$i]); echo $keyval[0] . " ==== " . $keyval[1] . " ---- "; }

This exploded out both by ‘&’ and then by ‘=’ which helped me to get to the base64 ended image

It was my guess, but I wasn’t sure… I would leave content type haeader to corona to figure out.

I just tried, this works:

main.lua:

network.request("http://localhost/test.php","POST", function( e ) &nbsp; &nbsp; print(e.response) end, { &nbsp; &nbsp; body = "message=hey", })

test.php

\<?php var\_dump($\_POST); ?\>

Prints out:

array(1) { &nbsp; &nbsp; ["message"]=\> &nbsp; &nbsp; string(3) "hey" }

There is a pretty well known issue with PHP and $_POST array’s being empty. It’s not a Corona issue, but a PHP and hosting issue.  If you Google “PHP $_POST empty”:

https://www.google.com/search?q=php+%24_post+empty&oq=php+%24_POST

You will get a bunch of hits about this.

Rob

Thanks for coming back Vlad - I’ve sorted it between then and now - turns out it was down to the url form encoding. I needed to change the Content-type to application/x-www-form-urlencoded

Agreed Rob (obviously).

Ended up using the PHP:

$sets = explode('&', file\_get\_contents('php://input')); for($i=0;$i\<count($sets); $i++) { $keyval = explode('=', $sets[$i]); echo $keyval[0] . " ==== " . $keyval[1] . " ---- "; }

This exploded out both by ‘&’ and then by ‘=’ which helped me to get to the base64 ended image

It was my guess, but I wasn’t sure… I would leave content type haeader to corona to figure out.