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.
rob
2
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) if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "+") end return str end
now putting it all together:
local function urlencode(str) if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "+") end return str end 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
rob
4
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) if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "+") end return str end
now putting it all together:
local function urlencode(str) if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "+") end return str end 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