[Tutorial] Using the HTTP module!

Share your Snippets
User avatar
DevDavisNunez
Site Admin
Posts: 18
Joined: Thu Nov 03, 2016 7:15 pm
Location: Mexico
Contact:

[Tutorial] Using the HTTP module!

Postby DevDavisNunez » Mon Jan 30, 2017 7:29 pm

Hello everyone!

On this occasion, we will talk about the http, protocol, use, ideas, and their implementation in ONElua.

About the protocol:
The Hypertext Transfer Protocol (HTTP) is a transaction-oriented protocol and follows the request-response schema between a client and a server. The client (usually called a "user agent") makes a request by sending a message, with a certain format to the server. The server (usually called a "web server") sends a response message.
Request-response are plain text making it more readable and easier to debug. This has the disadvantage of making the messages longer.
Finally the HTTP protocol, allows different forms of request:

GET:
Usually used to request a resource or file via the url, although it allows to send parameters of the same way in the uri,
example of it?
"Uri Simple"

Code: Select all

http:/onelua.x10.mx/index.php

"Uri with parameters"

Code: Select all

http:/onelua.x10.mx/index.php?page=main&lang=en


POST:
Usually used to send data of a form, unlike the get method, which only allows to send data with a long limit number, this method allows to send data without limit of long and without the reestrincion of "url encode", as are the spaces And the tildes.
"Uri"

Code: Select all

http:/onelua.x10.mx/index.php


Send request ONElua:
Image

Code: Select all

--[[
   Example of send data from POST and GET methods.
   Licensed by Creative Commons Attribution-ShareAlike 4.0
   http://creativecommons.org/licenses/by-sa/4.0/
   
   Designed by DevDavisNunez of Team ONElua for the use sample.
]]

-- Remember to change "http://devonelua.x10.mx/sample_http.php", for your own hosting, and the path to your script.

-- If you wish to wait without displaying or performing any action, simply delete the function "onNetGetFile".
function onNetGetFile(size,written,speed) -- Callback that runs while you get the answer of the server.
   screen.print(10,10,"Downloading...")
   screen.print(10,30,"Size: "..tostring(size).." Written: "..tostring(written).." Speed: "..tostring(speed).."Kb/s")
   screen.print(10,50,"Porcent: "..math.floor((written*100)/size).."%")
   draw.fillrect(0,520,((written*960)/size),24,color.new(0,255,0))
   screen.flip()
   buttons.read()
   if buttons.circle then   return 0 end --Cancel or Abort
   return 1;
end

color.loadpalette() -- Load Defaults colors
local res = wlan.connect() -- Try connect to any Access Point.
if res != 1 then -- If there is no internet connection, you abort.
   os.message("Connect to some internet and then retry using the sample!")
   os.exit()
end
local recv_post = nil -- Data of the post response.
local recv_get = nil -- Data of the get response.
local res_get,res_post = 0,0 -- Result state of response´s
local len_get,len_post = 0,0 -- Len of respose´s

local __Version = "V1R0" -- Version to send to the host, you can send number or string, but translate to string xD
while true do -- Main loop
   buttons.read() -- Read Buttons

   screen.print(480,10,"Http Sample ONElua",1,color.white,color.blue,__ACENTER)
   screen.print(10,30,"wlan.connect(): "..tostring(res))
   screen.print(10,50,"wlan.isconnected(): "..tostring(wlan.isconnected()))
   screen.print(10,70,"http.get(): "..tostring(res_get))
   screen.print(10,90,"http.post(): "..tostring(res_post))
   screen.print(10,110,"Press cross to send request GET")
   screen.print(10,130,"Press square to send request POST")

   screen.print(10,150,"GET response:\n"..tostring(recv_get))
   screen.print(10,240,"POST response:\n"..tostring(recv_post))
   screen.flip() -- Show Buff

   if buttons.cross then
      -- In request GET only send the 'var' - 'version' that is via URI or URL
      recv_get, len_get, res_get = http.get("http://devonelua.x10.mx/sample_http.php?version="..tostring(__Version))
   end

   if buttons.square then
      -- In request POST you can send GET vars, and POST vars, In both cases, the variables are linked with a '&', example user=Davis&pass=Secret xD&team=ONElua
      -- In the post data i try send a username from os.nick and score from number random.
      recv_post, len_post, res_post = http.post("http://devonelua.x10.mx/sample_http.php?version="..tostring(__Version), "username="..tostring(os.nick()).."&".."score="..tostring(os.time()))
   end
   if buttons.released.start then
      recv_post, recv_get = nil, nil
      break
   end
end


Receive requests PHP:

Code: Select all

<?
/*
   Example of receiving data from POST and GET methods.
   Licensed by Creative Commons Attribution-ShareAlike 4.0
   http://creativecommons.org/licenses/by-sa/4.0/
   
   Designed by DevDavisNunez of Team ONElua for the use sample.
*/

/*
   To read the data sent by the "GET" method,
   We must read from the global variable "$_GET", where index is the name of the variable we want to receive.
*/
/*
   To read the data sent by the "_POST" method,
   We must read from the global variable "$_POST", where index is the name of the variable we want to receive.
*/

$version = $_GET['version']; // Get the value of the parameter 'version' from the uri.
$username = $_POST['username']; // Get the value of the parameter 'username' from the post data.
$score = $_POST['score']; // Get the value of the parameter 'score' from the post data.

// Send a response, returning the same values received ...
// Format the output, to make line breaks and add a description of each variable.
echo "You entered:\n";
echo "Version: " . $version . "\n"; // Concatenate the variables with the text, with '.'
echo "Username = " . $username . "\n";
echo "Score = " . $score;

/*
   Whats Next?
   You can add more varibales,
   With the variables here, you can store a database,
   Create another script, draw in an img, the best 10 scores,
   Add the support of asking for a username, and return in image form your score, name, and level xD
*/
?>


I think that's it for now,
Remember to see the behavior of the sample, try to imagine ways of using it,
Formerly I use it for a system of comments in ONElua PSP,
In the same way can be used to connect to the whole web, imagine, and create !.
Image
There are only two options, play with the code or that the code plays with you.

Return to “Snippet Code”

Who is online

Users browsing this forum: No registered users and 1 guest