This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

http.request more then 1 command in the same session
#1
How to execute more then one http request in the same web sessions?


Code:
12345678910111213
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_accessend 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.
Reply
#2
Hi,

You probably get a response in the first request that you need to add to the headers of the second request.
Reply
#3
(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?
Reply
#4
Erwin is correct. Try logging response headers:

Code:
123
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/showthrea...74#pid5774
Reply
#5
(19.02.2019, 08:12)admin Wrote: Erwin is correct. Try logging response headers:

Code:
123
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/showthrea...74#pid5774
Thanx admin and Erwin Smile

This it the hdrs output:
* table: [set-cookie] * string: SESSION=23136A000858325C; [content-type] * string: text/plain
Reply
#6
So then you just need to pass cookie header to the next request:
Code:
123456789101112131415161718192021222324252627
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
Reply
#7
(19.02.2019, 09:16)admin Wrote: So then you just need to pass cookie header to the next request:
Code:
123456789101112131415161718192021222324252627
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!!
Reply


Forum Jump: