0

I’m trying to make a http.request to have a graph created, and then save the resulting .png graph image that is created. The problem is I want to do this with Lua, yet I’m struggling on two parts. (If you take url, you’ll see that this should work fine in a standard browser)

  1. Handling a 301 error, have looked through SO, I could see a few references to this and the need to use luasec, which I believe I have.

301 moved permanently with socket.http

Here is the script, with the URL I’m trying to call via HTTP, and then (eventually want to ) save the resulting graph image (.png file) that’s created


local http = require "socket.http" 
--local https = require("ssl.https")
local ltn12 = require "ltn12"
r = {} -- init empty table
local result, code, headers, status = http.request{
url="http://www.chartgo.com/create.do?charttype=line&width=650&height=650&chrtbkgndcolor=white&gridlines=1&labelorientation=horizontal&title=Fdsfsdfdsfsdfsdfsdf&subtitle=Qrqwrwqrqwrqwr&xtitle=Cbnmcbnm&ytitle=Ghjghj&source=Hgjghj&fonttypetitle=bold&fonttypelabel=normal&gradient=1&max_yaxis=&min_yaxis=&threshold=&labels=1&xaxis1=Jan%0D%0AFeb%0D%0AMar%0D%0AApr%0D%0AMay%0D%0AJun%0D%0AJul%0D%0AAug%0D%0ASep%0D%0AOct%0D%0ANov%0D%0ADec&yaxis1=20%0D%0A30%0D%0A80%0D%0A90%0D%0A50%0D%0A30%0D%0A60%0D%0A50%0D%0A40%0D%0A50%0D%0A10%0D%0A20&group1=Group+1&viewsource=mainView&language=en§ionSetting=§ionSpecific=§ionData=",
sink = ltn12.sink.table( r )
}

print("code=".. tostring(code))
print("status=".. tostring(status))
print("headers=".. tostring(headers))
print("result=".. tostring(result))
print("sink= ".. table.concat( r, "" ) )
print(result, code, headers, status )

for i,v in pairs(headers) do
  print("\t",i, v)
end

Which returns the 301 Moved Permanently error, plus via a viewer it also provides me with a link to another URL (this time a https on)

So to try and get to the https site first off, I tried adding in the ssl.http element with the following, but that that does not return anything at all, all nil .

local https = require("ssl.https")
local ltn12 = require "ltn12"
r = {} -- init empty table
local result, code, headers, status = https.request{
url="https://www.chartgo.com/create.do?charttype=line&width=650&height=650&chrtbkgndcolor=white&gridlines=1&labelorientation=horizontal&title=Fdsfsdfdsfsdfsdfsdf&subtitle=Qrqwrwqrqwrqwr&xtitle=Cbnmcbnm&ytitle=Ghjghj&source=Hgjghj&fonttypetitle=bold&fonttypelabel=normal&gradient=1&max_yaxis=&min_yaxis=&threshold=&labels=1&xaxis1=Jan%0D%0AFeb%0D%0AMar%0D%0AApr%0D%0AMay%0D%0AJun%0D%0AJul%0D%0AAug%0D%0ASep%0D%0AOct%0D%0ANov%0D%0ADec&yaxis1=20%0D%0A30%0D%0A80%0D%0A90%0D%0A50%0D%0A30%0D%0A60%0D%0A50%0D%0A40%0D%0A50%0D%0A10%0D%0A20&group1=Group+1&viewsource=mainView&language=en§ionSetting=§ionSpecific=§ionData=",
sink = ltn12.sink.table( r )
}

print("code=".. tostring(code))
print("status=".. tostring(status))
print("headers=".. tostring(headers))
print("result=".. tostring(result))
print("sink= ".. table.concat( r, "" ) )
print(result, code, headers, status )

And then …

  1. assuming I can eventually make the http.request work, the web page returns a png. image of the resulting graph - I’d love to be able to extract/copy that for further use within this piece of code..

As always any help/advice would be appreciated..

nodecentral
  • 446
  • 3
  • 16
  • just match `location` and make a request for link – Mike V. Nov 26 '21 at 15:24
  • Sorry @MikeV., what location ? I’ve tried the direct url via http and redirect url via https, but neither connect successfully http = 301, https = nil? – nodecentral Nov 27 '21 at 00:44
  • Server фnswer 301, 302 is a redirect to another link, in the response header necessarily there is a Location field to which you need to go. – Mike V. Nov 27 '21 at 10:24
  • Second: there is support for redirect=true in the header https.request() .. but in later versions of Lua – Mike V. Nov 27 '21 at 10:33
  • Thanks @MikeV. Ok, yes I can see the `location` In the headers and it’s `location https://www.chartgo.com/create.do?` - but how best do I handle that, do I simply pass that location url to a new https.request call (like my second part above) ? – nodecentral Nov 27 '21 at 16:04
  • Yes, just make another request using the `headers["Location"]` , but in your case it is a endless looped redirection, bad case – Mike V. Nov 27 '21 at 16:30
  • Hi @MikeV. Many thanks, thats that seems to have progressed me along further as the second http call now seems to work, as it returns a http 200 code too :-). Next thing is to grab the resulting .png file that is presented on the page. Any ideas ? – nodecentral Nov 27 '21 at 20:56
  • FYI - Using this, `k,v in pairs(headers) do print("\t",k, v) end` I can’t see anything about images in the new headers ? – nodecentral Nov 27 '21 at 21:00
  • i have endless looped redirection, does not return code 200 ,maybe bad url in the question? – Mike V. Nov 28 '21 at 08:07
  • Hi @MikeV. - yes you’re right something must be occurring differently, as it did work for me, but I’m also getting 301 messages now.. - I’ll see if I can replicate what I did before to get the 200 message… – nodecentral Nov 29 '21 at 11:22

0 Answers0