Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call C# functions from anywhere using URL (HTTP server)
#6
QM client examples.

Macro Macro3182
 
Code:
Copy      Help
LaHttpCall("Simple") ;;"http://localhost:4455/Simple"

LaHttpCall("http://:password278351@localhost:4455/Simple");

str r1 = LaHttpCall("Returns")
out r1

LaHttpCall("Parameters" "a=aa" "c=10" "d=true")

str text = "any text"
str path = "C:\Test\test.txt"
LaHttpCall("Parameters" F"a={text}" F"@b={path}")

int x(5) y(2)
int r2 = val(LaHttpCall("Add" F"x={x}" F"y={y}"))
out r2

Function LaHttpCall
Code:
Copy      Help
;/
function~ ~url [~a1] [~a2] [~a3] [~a4] [~a5] [~a6] [~a7] [~a8] [~a9] [~a10]

;Calls a C# function on a computer where <link "https://www.libreautomate.com/forum/showthread.php?tid=7468">LibreAutomate HTTP server</link> is running.

;url - URL like "http://HOST:4455/FunctionName".
;;;;HOST can be: localhost (this computer); IP; LAN computer name; web server (like www.example.com).
;;;;Password can be specified like "http://:password@HOST:4455/FunctionName".
;;;;If the HTTP server is on the same computer and don't need a password, can be just function name.
;a1-a10 - C# function parameters, like "name=value". To send a file, use "@name=filepath". Parameters can be in any order, optionals omitted. Don't need urlencoding.

;EXAMPLES
;LaHttpCall("Simple") ;;"http://localhost:4455/Simple"
;
;LaHttpCall("http://:password278351@localhost:4455/Simple");
;
;str r1 = LaHttpCall("Returns")
;out r1
;
;LaHttpCall("Parameters" "a=aa" "c=10" "d=true")
;
;str text = "any text"
;str path = "C:\Test\test.txt"
;LaHttpCall("Parameters" F"a={text}" F"@b={path}")
;
;int x(5) y(2)
;int r2 = val(LaHttpCall("Add" F"x={x}" F"y={y}"))
;out r2


if(findrx(url "^https?://")<0) url-"http://localhost:4455/"
ARRAY(str) a
if(findrx(url F"^https?://(?:(.*?):(.*?)@)?(\[.+?\]|[^\s:/?]+)(:\d+)?/(\w+)$" 0 0 a)<0) end F"{ERR_BADARG} url"

if(a[2].len+a[1].len>0) str auth.from("Authorization: Basic " _s.encrypt(4 F"{a[1]}:{a[2]}"))

Http http.Connect(a[3] "" "" iif(a[4].len val(a[4]+1) iif(url[4]='s' 443 0)))
int i e ok; str r h sn sv
str* p=&a1
for i 0 getopt(nargs)-1
,str& s=p[i]
,e=findc(s '='); if(e<1) end F"{ERR_BADARG} a{i+1}. Must be name=value."
,int isFile=s[0]='@'
,http.PostAdd(sn.get(s isFile e-isFile) sv.get(s e+1) isFile)
if(!http.PostFormData(a[5] 0 r auth 0 0 0 h)) end F"{ERR_FAILED}. {http.lasterror}"
_s.get(h 9 findc(h 13)-9)
if(!_s.beg("200 ")) if(r.len) end F"{ERR_FAILED}. {r}"; else end F"{ERR_FAILED}. HTTP status: {_s}"
ret r

Changes
2023-06-22:
- Supports error descriptions in response body, to match the updated HTTP server code.


Messages In This Thread
RE: Call C# functions from anywhere using URL (HTTP server) - by Gintaras - 06-07-2023, 05:47 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)