Network.request params.body

Hi guys I am new to Corona and I have been spending for few days to figure out how the network.request api work. 

Here is one of the questions I need your helps. 

<html>
<body>
<FORM action=“http://ex.org/process” method=“get”>
<INPUT type=“text” name=“fileName” value=“http://example.org/images/test.png”>
<INPUT type=“submit” value=“Send”>
</FORM>
</body>
</html>

How do I insert html above into header and body params in corona?

This is what I got so far…

local headers = {}

headers[“Content-Type”] = “text/plain” – is this correct?

local params = {}

params.headers = headers

params.body = "what should I inside here?"

network.request( “http://ex.org/process”, “GET”, networkListener, params)

Any help would be much appreciated and sorry for my bad english.

First you wouldn’t actually use the HTML.  It’s pretty simple actually:

local URL = "http://ex.org/process?fileName=http://example.org/images/test.png" network.request(URL, "GET", networkListener)

There is only one gotcha here.  You need to URL encode the parameters so you will need a urlencode() function.

local function urlencode(str) &nbsp; if (str) then &nbsp;&nbsp;&nbsp; str = string.gsub (str, "\n", "\r\n") &nbsp;&nbsp;&nbsp; str = string.gsub (str, "([^%w])", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function (c) return string.format ("%%%02X", string.byte(c)) end) &nbsp;&nbsp;&nbsp; str = string.gsub (str, " ", "+") &nbsp; end &nbsp; return str&nbsp;&nbsp; &nbsp; end

now putting it all together:

local function urlencode(str) &nbsp; if (str) then &nbsp;&nbsp;&nbsp; str = string.gsub (str, "\n", "\r\n") &nbsp;&nbsp;&nbsp; str = string.gsub (str, "([^%w])", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function (c) return string.format ("%%%02X", string.byte(c)) end) &nbsp;&nbsp;&nbsp; str = string.gsub (str, " ", "+") &nbsp; end &nbsp; return str&nbsp;&nbsp; &nbsp; end &nbsp; local URL = "http://ex.org/process?fileName=" .. urlencode("http://example.org/images/test.png") network.request(URL, "GET", networkListener)

it works…thank rob for answering my question.

Now I got another question…
How do I upload and post by using network.request?

Corresponding HTML form from server side:

</form> <form action=“process” method=“post” enctype=“multipart/form-data”>   <input type=“file” name=“f”/> <input type=“submit”/>   </form>
local headers = {}

headers[“Content-Type”] = “multipart/form-data” 

local params = {}

params.headers = headers

params.body =

{

   filename = "test.jpg,

   baseDirectory = system.ResourcesDirectory,

}

network.request( “http://ex.org/process”, “POST”, networkListener, params)

I tried code above with no luck. Am I missing something? or it should not be done in this way? Please help…thank you

First you wouldn’t actually use the HTML.  It’s pretty simple actually:

local URL = "http://ex.org/process?fileName=http://example.org/images/test.png" network.request(URL, "GET", networkListener)

There is only one gotcha here.  You need to URL encode the parameters so you will need a urlencode() function.

local function urlencode(str) &nbsp; if (str) then &nbsp;&nbsp;&nbsp; str = string.gsub (str, "\n", "\r\n") &nbsp;&nbsp;&nbsp; str = string.gsub (str, "([^%w])", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function (c) return string.format ("%%%02X", string.byte(c)) end) &nbsp;&nbsp;&nbsp; str = string.gsub (str, " ", "+") &nbsp; end &nbsp; return str&nbsp;&nbsp; &nbsp; end

now putting it all together:

local function urlencode(str) &nbsp; if (str) then &nbsp;&nbsp;&nbsp; str = string.gsub (str, "\n", "\r\n") &nbsp;&nbsp;&nbsp; str = string.gsub (str, "([^%w])", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function (c) return string.format ("%%%02X", string.byte(c)) end) &nbsp;&nbsp;&nbsp; str = string.gsub (str, " ", "+") &nbsp; end &nbsp; return str&nbsp;&nbsp; &nbsp; end &nbsp; local URL = "http://ex.org/process?fileName=" .. urlencode("http://example.org/images/test.png") network.request(URL, "GET", networkListener)

it works…thank rob for answering my question.

Now I got another question…
How do I upload and post by using network.request?

Corresponding HTML form from server side:

</form> <form action=“process” method=“post” enctype=“multipart/form-data”>   <input type=“file” name=“f”/> <input type=“submit”/>   </form>
local headers = {}

headers[“Content-Type”] = “multipart/form-data” 

local params = {}

params.headers = headers

params.body =

{

   filename = "test.jpg,

   baseDirectory = system.ResourcesDirectory,

}

network.request( “http://ex.org/process”, “POST”, networkListener, params)

I tried code above with no luck. Am I missing something? or it should not be done in this way? Please help…thank you