I’m sure this is not a unique question about Lua 5.1 (especially an embedded instance, where you can’t install modules, your only options is pure Lua files/reference, therefore I can’t install something like luacurl ). I’ve seen this matter referred to in various SO places, and tried them, but I can’t seem to find a fix to make an https file download request work in this particular Lua environment..
To give you an example, the code below aims to download 2 different files, the http call works fine, returning a 200 code, but the https one doesn’t, it doesn’t return anything ?
Both targets can be access directly via the browser..
Please could some highlight what I’m missing in the https call to make it work?
print("--------DOWNLOAD http---------")
local http = require("socket.http")
local body, code = http.request("http://pbs.twimg.com/media/CCROQ8vUEAEgFke.jpg")
print(code)
if not body then error(code) end
local f = assert(io.open('mnt/nas/webtest1.jpg', 'wb')) -- open in "binary" mode
f:write(body)
f:close()
print("--------DOWNLOAD https---------")
local https = require "ssl.https" --luasec
local body, code = https.request("https://u.cubeupload.com/jbcooper/16146313918060.jpg")
print(code)
if not body then error(code) end
local f = assert(io.open('mnt/nas/webtest2.jpg', 'wb')) -- open in "binary" mode
f:write(body)
f:close()
As requested, the following returns nothing at all..
local https = require "ssl.https" --luasec
print(https.request("https://u.cubeupload.com/jbcooper/16146313918060.jpg"))
If it helps, if I run the following it returns 0.4
local https = require("ssl.https")
local httpsVersion = string.sub(https._VERSION,1,3)
print (httpsVersion)
Related
I want to get a value out of a website.
I want to get the ARS$ 3.435,63 out of this div and put it into a varible to calculate with.
<span class="market_commodity_orders_header_promote">ARS$ 3.435,63</span>
I have searched up some stuff but couldn't find anything to help me so if anybody know how to to this in lua that will be nice.
note: I am intermediate at lua
I have not tried "luasocket" because the download page is gone
To download a webpage using only the lua standard library, you can call curl if your system has it installed.
-- Download webpage
local file = assert(io.popen("curl site.name.com", "r"))
local htmlText = assert(f:read("*a"))
file:close()
-- Find commodity cost
local _, _, cost = htmlText:find("<span class=\"market_commodity_orders_header_promote\">(ARS\$ [0-9.,]+)<\/span>")
print(cost) -- Should print "ARS$ 3.435,63"
I’m trying to write a custom plugin to transform response body. I could’ve used a response transformer plugin, but my response body json is complex, so I want to remove few fields from it.
I tried using post-function plugin to write my custom lua code but it doesn’t let me import cjson, so I’m unable to decode the response and remove specific keys from it.
My lua code in body_filter:
local cjson = require(“cjson”)
local body = cjson.decode(kong.response.get_raw_body())
-- set custom key’s value to 1
body.subKeyFoo.subSubKey = 1;
This is what I get:
require cjson not allowed within sandbox “kong”
The sandbox is enabled, this is to protect arbitrary Lua code from doing dangerous things. See the docs on how to disable the sandbox. Link: https://docs.konghq.com/gateway/latest/reference/configuration/#untrusted_lua check untrusted_lua_xxx options (3 in total)
Lua beginner here. :)
I am trying to load a file by url and somehow I am just too stupid to get all the code samples here on SO to work for me.
How to download a file in Lua, but write to a local file as it works
downloading and storing files from given url to given path in lua
socket = require("socket")
http = require("socket.http")
ltn12 = require("ltn12")
local file = ltn12.sink.file(io.open('test.jpg', 'w'))
http.request {
url = 'http://pbs.twimg.com/media/CCROQ8vUEAEgFke.jpg',
sink = file,
}
my program runs for 20 - 30s and afterwards nothing is saved. There is a created test.jpg but it is empty.
I also tried to add w+b to the io.open() second parameter but did not work.
The following works:
-- retrieve the content of a URL
local http = require("socket.http")
local body, code = http.request("http://pbs.twimg.com/media/CCROQ8vUEAEgFke.jpg")
if not body then error(code) end
-- save the content to a file
local f = assert(io.open('test.jpg', 'wb')) -- open in "binary" mode
f:write(body)
f:close()
The script you have works for me as well; the file may be empty if the URL can't be accessed (the script I posted will return an error in this case).
I am trying to call a web service written in C#. Below is client side in corona:
local client = require "soap.client"
local ns, meth, ent = client.call {
url = "http://172.16.1.162:7878/BaksetBilgi.svc",
soapaction = "doubler",
method = "http://titck.kara.com/bask",
entries = {
......}
When I run this code, it gives error :
module 'soap.client' not found:resource (soap.client.lu) does not exist in archive..
How can I solve it?
That module is not part of corona. Looks like it's only 3 files, you could copy them into your Corona project. Note that the language that SOAP service is written in is irrelevant to the client: SOAP is a protocall, if you know how to format your data in HTML then you can do SOAP. Look at any good SOAP tutorial. But I don't know why you wouldn't want to use that library, just copy the files into your project.
I'm new with lua but working on an application that works on specific files with given path. Now, I want to work on files that I download. Is there any lua libraries or line of codes that I can use for downloading and storing it on my computer ?
You can use the LuaSocket library and its http.request function to download using HTTP from an URL.
The function has two flavors:
Simple call: http.request('http://stackoverflow.com')
Advanced call: http.request { url = 'http://stackoverflow.com', ... }
The simple call returns 4 values - the entire content of the URL in a string, HTTP response code, headers and response line. You can then save the content to a file using the io library.
The advanced call allows you to set several parameters like HTTP method and headers. An important parameter is sink. It represents a LTN12-style sink. For storing to file, you can use sink.file:
local file = ltn12.sink.file(io.open('stackoverflow', 'w'))
http.request {
url = 'http://stackoverflow.com',
sink = file,
}