This works with the PowerShell code. Not tested with Tasker, FRP etc.
Member function TcpSocket.HttpRead
Macro test HttpRead
HTTP client in PowerShell
Member function TcpSocket.HttpRead
;/test HttpRead
function! str&method str&path str&headers [str&body]
;Reads HTTP message.
;Returns: 1 if OK, 0 if failed.
;method - receives the HTTP method string, like "GET" or "POST".
;path - receives the request target, like "/" or "/file.html".
;headers - receives the request headers. Raw.
;body - if used, receives the request body. For example the POST data. Raw.
;REMARKS
;If body data is sent, the request headers should contain Content-Length. Data must not be chunked or compressed.
str s
int dataStart flags
;g1
int r = Receive(s 8100 0 flags)
if(r=2) goto gserverError ;;timeout
dataStart=find(s "[][]")
if(dataStart<0)
,if(r=0) goto gBadRequest
,flags=1
,goto g1 ;;wait for complete headers
dataStart+4
ARRAY(str) m
if(findrx(s "^(GET|POST|PUT|PATCH|HEAD|DELETE) (.+) HTTP/1\.1[](?s)(.+?[])[]" 0 0 m)<0) goto gBadRequest
method.swap(m[1])
path.swap(m[2])
headers.swap(m[3])
if(r=1 and findrx(headers "(?m)^Content-Length: *(\d+)" 0 1 _s 1)>=0)
,int conLen=val(_s)
,rep
,,int read=conLen-(s.len-dataStart)
,,if(read<1) break
,,r = Receive(s read 0 1)
,,if(r=0) break
,,if(r=2) goto gserverError ;;timeout
if(&body) body.get(s dataStart)
ret 1
;gBadRequest
Send("HTTP/1.1 400 Bad Request")
ret
;gserverError
Send("HTTP/1.1 500 Internal Server Error")
ret
Macro test HttpRead
if(getopt(nthreads)>1) ret ;;allow single instance
AddTrayIcon "cut.ico" "test_TcpSocket_server[]Ctrl+click to end."
#compile "__TcpSocket"
TcpSocket x.ServerStart(5032 &sub.OnClientConnected)
#sub OnClientConnected
function TcpSocket&client $clientIp hDlg !*reserved
;This function is called in server side, when a client connects.
;This function runs in separate thread for each client connection.
out F"<><z 0xc0ffff>SERVER: client connected:</z> {clientIp}"
str method path headers data
if(!client.HttpRead(method path headers data)) out "HttpRead failed"; ret
out method
out path
out headers
out data
out data.len
client.Send("HTTP/1.1 200 OK[]Connection: close[][]")
err+ out _error.description
HTTP client in PowerShell
cls
$filePath = "C:\Test\customers.sql"
$fileBytes = [System.IO.File]::ReadAllBytes($filePath)
$fileBase64 = [Convert]::ToBase64String($fileBytes)
$formData = @{
"fileName" = "Doc.docx"
"fileBase64" = $fileBase64
}
Invoke-RestMethod -Uri "http://127.0.0.1:5032/saveFile" -Method Post -Body $formData -ContentType 'multipart/form-data'
#Invoke-RestMethod -Uri "http://127.0.0.1:5032/saveFile" -Method Get