Logic Machine Forum
http.request more then 1 command in the same session - 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.request more then 1 command in the same session (/showthread.php?tid=1921)



http.request more then 1 command in the same session - gjniewenhuijse - 18.02.2019

How to execute more then one http request in the same web sessions?


Code:
function openDoor(iLoxxId)
  require('socket.http')
 socket.http.TIMEOUT = 5
 
  local urlLogin = 'http://'..wl_IP..'/login.cgi?Source=Webpage&Username='..wl_USR..'&Password='..wl_PWD
 local urlAccess = 'http://'..wl_IP..'/setRemoteAccess.cgi?Source=Webpage&LoxxId='..iLoxxId..'&Action=Start'
  wl_login_data = socket.http.request(urlLogin)
  log(wl_login_data)
  wl_access = socket.http.request(urlAccess)
  log(wl_access) 
end

openDoor(19)

I have a request for  login and another request for opening the door. But now i received: 401 Authorization Required

If i execute both statements in the browser it works.


RE: http.request more then 1 command in the same session - Erwin van der Zwart - 18.02.2019

Hi,

You probably get a response in the first request that you need to add to the headers of the second request.


RE: http.request more then 1 command in the same session - gjniewenhuijse - 19.02.2019

(18.02.2019, 23:02)Erwin van der Zwart Wrote: Hi,

You probably get a response in the first request that you need to add to the headers of the second request.

Maybe, but when i execute this statements in the same browser tab it works, so i think it has nothing todo with headers. 

Or how to find this out?


RE: http.request more then 1 command in the same session - admin - 19.02.2019

Erwin is correct. Try logging response headers:

Code:
url = ...
res, code, hdrs, stat = socket.http.request(url)
log(hdrs)

Logged headers should contain "set-cookie" field which then should be parsed and passed as "cookie" header to next request. This post has a similar example though first request is different since login token is in response JSON body: https://forum.logicmachine.net/showthread.php?tid=993&pid=5774#pid5774


RE: http.request more then 1 command in the same session - gjniewenhuijse - 19.02.2019

(19.02.2019, 08:12)admin Wrote: Erwin is correct. Try logging response headers:

Code:
url = ...
res, code, hdrs, stat = socket.http.request(url)
log(hdrs)

Logged headers should contain "set-cookie" field which then should be parsed and passed as "cookie" header to next request. This post has a similar example though first request is different since login token is in response JSON body: https://forum.logicmachine.net/showthread.php?tid=993&pid=5774#pid5774
Thanx admin and Erwin Smile

This it the hdrs output:
* table: [set-cookie] * string: SESSION=23136A000858325C; [content-type] * string: text/plain


RE: http.request more then 1 command in the same session - admin - 19.02.2019

So then you just need to pass cookie header to the next request:
Code:
require('socket.http')
socket.http.TIMEOUT = 5

url1 = ...
url2 = ...

res, err, hdrs = socket.http.request(url1)

if res then
  tbl = {}
  res, err = socket.http.request({
    url = url2,
    headers = {
      cookie = hdrs['set-cookie']
    },
    sink = ltn12.sink.table(tbl),
  })

  if res then
    resp = table.concat(tbl)
    log(resp)
  else
    alert('request 2 failed: ' .. tostring(err))
  end
else
  alert('request 1 failed: ' .. tostring(err))
end



RE: http.request more then 1 command in the same session - gjniewenhuijse - 19.02.2019

(19.02.2019, 09:16)admin Wrote: So then you just need to pass cookie header to the next request:
Code:
require('socket.http')
socket.http.TIMEOUT = 5

url1 = ...
url2 = ...

res, err, hdrs = socket.http.request(url1)

if res then
 tbl = {}
 res, err = socket.http.request({
   url = url2,
   headers = {
     cookie = hdrs['set-cookie']
   },
   sink = ltn12.sink.table(tbl),
 })

 if res then
   resp = table.concat(tbl)
   log(resp)
 else
   alert('request 2 failed: ' .. tostring(err))
 end
else
 alert('request 1 failed: ' .. tostring(err))
end

Yeah, thanks it works!!