Logic Machine Forum
HTTP Post - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: HTTP Post (/showthread.php?tid=453)



HTTP Post - Desislav - 04.11.2016

Hello,
How can I send POST command "POST /relay/0?turn=on" from lua script?


Thanks
Deso


RE: HTTP Post - admin - 05.11.2016

One of these requests should work for you, don't forget to change the IP address as required.
Code:
local http = require('socket.http')

-- empty post body
http.request('http://192.168.1.1/relay/0?turn=on', '')

-- post instead of get
http.request('http://192.168.1.1/relay/0', 'turn=on')

-- post and get
http.request('http://192.168.1.1/relay/0?turn=on', 'turn=on')



RE: HTTP Post - Desislav - 05.11.2016

Thanks, it work. Is there any manual or examples about http post and get?


RE: HTTP Post - admin - 07.11.2016

Here's the library reference:
http://w3.impa.br/~diego/software/luasocket/http.html#request


RE: HTTP Post - Gadjoken - 09.05.2019

(07.11.2016, 07:17)admin Wrote: Here's the library reference:
http://w3.impa.br/~diego/software/luasocket/http.html#request

Hello Admin,
I would like to send by order on a second LM or on another box (Box TV) i use this script but the command is not sent.
I indicate the IP and the password but the request is not sent.
I try directly in the browser and it works.
these scripts are still functional?


local http = require('socket.http')

http.request('http://remote:remote@IP/scada-remote?m=json&r=grp&fn=write&alias=34/1/2&value=1', '')

thanks.
B.R.


RE: HTTP Post - Daniel - 09.05.2019

Hi
Are you referring to remote services? Sure they work, fallow this
https://forum.logicmachine.net/showthread.php?tid=550&pid=3009#pid3009
BR


RE: HTTP Post - admin - 09.05.2019

Add log call to see what the web server returned in Logs tab:

Code:
local http = require('socket.http')
res, err = http.request('http://remote:remote@IP/scada-remote?m=json&r=grp&fn=write&alias=34/1/2&value=1')
log(res, err)



RE: HTTP Post - Gadjoken - 09.05.2019

(09.05.2019, 10:09)admin Wrote: Add log call to see what the web server returned in Logs tab:

Code:
local http = require('socket.http')
res, err = http.request('http://remote:remote@IP/scada-remote?m=json&r=grp&fn=write&alias=34/1/2&value=1')
log(res, err)
Can be then admin login request ?

Log : 
* arg: 1
 * nil
* arg: 2
 * string: connection refused



RE: HTTP Post - admin - 09.05.2019

Connection refused means that URL has incorrect IP or port (optional, default 80). Can you ping that IP from System config > Status > Network utilities?


RE: HTTP Post - Gadjoken - 09.05.2019

(09.05.2019, 11:53)admin Wrote: Connection refused means that URL has incorrect IP or port (optional, default 80). Can you ping that IP from System config > Status > Network utilities?

it works with the local IP but not the public one I do not know why. I continue my tests. With the FREEPLAYER i have : 

* arg: 1
 * string:
* arg: 2
 * number: 403



RE: HTTP Post - admin - 09.05.2019

Do you mean that you use port forwarding? Are two devices in the same sub-network and you want to access another device via port-forwarded IP?


RE: HTTP Post - Gadjoken - 09.05.2019

(09.05.2019, 12:18)admin Wrote: Do you mean that you use port forwarding? Are two devices in the same sub-network and you want to access another device via port-forwarded IP?

No for example I have an LM in the same network as the player freebox and I would like to drive via the LM this player. 
So I have the following script (For the remote I did my tests for sending URLs because it did not work with the player, to be sure I was not wrong in the scripts).


Code:
   local http = require('socket.http')
        res, err = http.request('http://http://IPLOCAL/pub/remote_control?code=CODETELECOMMAND"&key=play')
    log(res, err)
--[[
"red" // Bouton rouge
"green" // Bouton vert
"blue" // Bouton bleu
"yellow" // Bouton jaune


"power" // Bouton Power
"list" // Affichage de la liste des chaines
"tv" // Bouton tv

"1" // Bouton 1
"2" // Bouton 2
"3" // Bouton 3
"4" // Bouton 4
"5" // Bouton 5
"6" // Bouton 6
"7" // Bouton 7
"8" // Bouton 8
"9" // Bouton 9

"back" // Bouton jaune (retour)
"0" // Bouton 0
"swap" // Bouton swap

"info" // Bouton info
"epg" // Bouton epg (fct+)
"mail" // Bouton mail
"media" // Bouton media (fct+)
"help" // Bouton help
"options" // Bouton options (fct+)
"pip" // Bouton pip

"vol_inc" // Bouton volume +
"vol_dec" // Bouton volume -

"ok" // Bouton ok
"up" // Bouton haut
"right" // Bouton droite
"down" // Bouton bas
"left" // Bouton gauche

"prgm_inc" //Bouton programme +
"prgm_dec" // Bouton programme -

"mute" // Bouton sourdine
"home" // Bouton Free
"rec" // Bouton Rec

"bwd" // Bouton << retour arrière
"prev" // Bouton |<< précédent
"play" // Bouton Lecture / Pause
"fwd" // Bouton >> avance rapide
"next" // Bouton >>| suivant
--]]

When I realize it directly in my browser it works.


RE: HTTP Post - admin - 09.05.2019

Your URL has http:// twice and there's a double quote character in code argument: code=CODETELECOMMAND"


RE: HTTP Post - Gadjoken - 09.05.2019

(09.05.2019, 13:05)admin Wrote: Your URL has http:// twice and there's a double quote character in code argument: code=CODETELECOMMAND"

Yes i have a single http lack of copy.
it's the " who's causing the problem...
(Sorry for making you waste yout time !!!!)
it works perfectly.

One las question concerning all of thse commands how can i easily reduce the script and not put if 1 then play if 2 then power if 3 .... etc...
can we recover the names of the custom values in order to put in my URL according to the command achieve?

Thanks a lot.
BR.


RE: HTTP Post - admin - 09.05.2019

There's no easy way of accessing custom values. But you can create a table with number - key mapping:
Code:
keys = {
  [0] = 'stop',
  [1] = 'play',
}

val = event.getvalue()
key = keys[ val ]

if key then
  local http = require('socket.http')
  res, err = http.request('http://IPLOCAL/pub/remote_control?code=CODETELECOMMAND&key=' .. key)
end



RE: HTTP Post - Gadjoken - 09.05.2019

(09.05.2019, 13:28)admin Wrote: There's no easy way of accessing custom values. But you can create a table with number - key mapping:
Code:
keys = {
 [0] = 'stop',
 [1] = 'play',
}

val = event.getvalue()
key = keys[ val ]

if key then
 local http = require('socket.http')
 res, err = http.request('http://IPLOCAL/pub/remote_control?code=CODETELECOMMAND&key=' .. key)
end

Thank you again for your answers.
B.R.


RE: HTTP Post - domobrain - 11.09.2023

Hi !!! 

Please, whear can I put the script for Http POST ??? In Resident ???

I have seen this:  https://forum.logicmachine.net/showthread.php?tid=453

local http = require('socket.http')

-- empty post body
http.request('http://192.168.1.1/relay/0?turn=on', '')

-- post instead of get
http.request('http://192.168.1.1/relay/0', 'turn=on')

Thanks in advance for your answer

Regards


RE: HTTP Post - Daniel - 11.09.2023

It depend of the application but it can be simple event script.


RE: HTTP Post - domobrain - 13.09.2023

(11.09.2023, 11:06)Daniel Wrote: It depend of the application but it can be simple event script.

Thanks, for your answer