[RESOLVED - Yay] I can't get a super basic HTTP POST network request to work

@Naomi, yay!

@rob, i wish you had seen this post earlier too. I hesitated to jumping in because I figured you would of shown up with better answers :slight_smile:

I will say though, I tried setting the headers(and so did naomi) without any love. I can’t remember the few I tried aside from the application/json but i tried one just as text. Despite doing that, when I inspected the headers on the response it came in as the content type you gave. Maybe if I had explictly set it to application/x-www-form-urlencoded it would have made a difference. Anyways, I don’t need to do anything with it so I guess i’m not that interested in checking it out :slight_smile: [import]uid: 147305 topic_id: 29545 reply_id: 118972[/import]

BTW, Rob @robmiracle, do you know how I may send json encoded data via HTTP POST to web-server? I will eventually need to send a big lua table (which is basically a game variable table) that I want to send back & forth, but it doesn’t seem like I can do this if I json.encode the lua table and send it as param.body via HTTP POST method.

Or, am I missing something super simple still?

Naomi [import]uid: 67217 topic_id: 29545 reply_id: 118973[/import]

So, when I json.encode the postdata.body, the var_dump($_REQUEST) returns:

[text]
RESPONSE: array(2) {
["“email”]=>
string(24) “bXllbWFpbEBteXdlYi5jb20=”
[“username”]=>
string(17) “bXluaWNrbmFtZQ==”"
}
[/text]

I’ll eventually have to learn how to parse this array properly so that I can use the data properly – but for now (unless someone knowledgeable would jump in and let me know how to parse this), I’ll focus on email validation, existing username check, etc., etc. Got to forge ahead one step at a time. There’s still a lot to do and learn after all.

Cheers,
Naomi
[import]uid: 67217 topic_id: 29545 reply_id: 118976[/import]

I’m guessing that data is base64 encoded (and if you followed my blog post on using REST api services, it probably is…)

You simply need to decode the data.

[code]
local mime = require(“mime”)

local username = mime.unb64(response.username)
local email = mime.unb64(response.email)

[/code] [import]uid: 19626 topic_id: 29545 reply_id: 118982[/import]

Thank you, Rob @robmiracle! Yes, I did follow your blog on REST api services. It’s a great tutorial. Super helpful.

About decoding, I’m wondering how I may decode the $_REQUEST that PHP side receives.

-- for example, when I encode postdata.body like this in Corona/LUA side  
postdata.body = json.encode("email="..set\_email.."&username="..set\_user)  
  
-- and when my PHP side looks like this  
<?php <br> echo var\_dump($\_REQUEST);  
?\>  
  
-- it returns the following on simulator terminal  
RESPONSE: array(2) {  
 [""email"]=\>  
 string(24) "bXllbWFpbEBteXdlYi5jb20="  
 ["username"]=\>  
 string(17) "bXluaWNrbmFtZQ==""  
}  

What I would like to know is how I may parse the email and username on PHP side so that I may handle it (such as checking to see if the username is already taken, or it’s okay to be inserted into the database, etc). When I don’t json-encode the postdata.body, I can get what I need and handle the data accordingly, because all I need to do then is

<?php <br>$email = base64\_decode($\_REQUEST['email']);  
$username = base64\_decode($\_REQUEST['username']);  
?\>  

But I don’t know how I may pull out what I need from $_REQUEST when the json-encoded postdata arrives to PHP. I know I have still much to learn. I’d be grateful if you could show me how I may do this anyhow.

Thanks again.

Naomi [import]uid: 67217 topic_id: 29545 reply_id: 118984[/import]

You don’t want to json encode the data you send, specially when its this simple:

postBody=“email=”…set_email…"&username="…set_user

Is all you need.

Webservers expect Key Value pairs. For GET requests, they are on the URL and start after the question mark like:

http://someserver.com/somescript.php ?key1=value1&key2=value2&key3=value3

Each Key-Value pair is separated by an &.

For POST requests, the data is part of the post body:

postBody = “key1=value1&key2=value2&key3=value3”

Then in PHP, you would use the $_POST[] array:

echo($_POST[“key1”]);
echo($_POST[“key2”]);
echo($_POST[“key3”]);

Now if you have to send some really complex data, like tables inside of tables, you might want to json.encode that table and then it would be something like:

in your app:

postBody = “data=” … mime.b64(json.encode(someTableofData))

make your request.

Then in your PHP script:

$data = json_decode(base64_decode($_POST[“data”]));

Now in your PHP script you would have an associative array (the same thing as a Lua table) that matches key for key, value for value what you started with in your app.

But if you’re just passing in a couple of variables, no need to add the JSON complexity.
[import]uid: 19626 topic_id: 29545 reply_id: 118987[/import]

Hey, Rob @robmiracle, thank you so much for the detailed explanation!!

Yes, I want to be able to send a lua table off to PHP. The reason why I tried to json.encode the simple data (just a couple of simple string variables) was because I always feel it’s important for me to understand the mechanics of things at the most basic level before tackling something more complex.

I’ll examine what you posted above when I do work on seinding a lua table (with lots of variables with additional tables nested inside them).

Thanks again! I so appreciate all your help.

Naomi [import]uid: 67217 topic_id: 29545 reply_id: 118989[/import]

Nice, I hope this thread gets simplified and turned into a Corona Tutorial. My head is spinning and I know it’s something good to share. [import]uid: 6084 topic_id: 29545 reply_id: 120254[/import]

Nice, I hope this thread gets simplified and turned into a Corona Tutorial. My head is spinning and I know it’s something good to share. [import]uid: 6084 topic_id: 29545 reply_id: 120254[/import]