Https.request simple and generic forms

I have been using this simple https.request format with success in my AAF application.

local res, code, headers, status = https.request(“https://myserver/modem_scripts/modemUpdate.php”,sendString)

The source in this case a string “sendString” is sent as a POST request using the default configuration of

default = {
protocol = "tlsv1",
options = "all",
verify = "none",
}

As TLSv1 is deprecated and unsecure I wanted to modify the request to use TLSv1.2 . This requires the generic form of the request which is LTN12 based.

The generic form is now:

local res, code, headers, status = https.request({
url = “https://myserver/modem_scripts/modemUpdate.php”,
protocol = ‘tlsv1_2’,
method = ‘POST’,
headers = {
[“content-type”] = “application/x-www-form-urlencoded”,
[“content-length”] = string.len(sendString)
},
sink = ltn12.sink.file(io.stdout),
source = ltn12.source.string(sendString)
})

References
LuaSocket HTTP support
LuaSocket LTN12 module

I am trying to download a file from url = https://dropbox.com/xxxxxxxxxxxdl=1 and save the file on the gateway to a local file using tlsv1_2 or tlsv1_3. From the above code it seems the following should work but I do not understand how sendString should be configured.

– downloadfile - downloads a file to the gateway

– Returns:
– result - result of this function
– filename - name of file
– fulllocalpath - path where file is stored locally
– resultstr - status info

local function downloadfile(filename, url, isbinary)
local string1 = " Downloading file from: " … url
–print(“UMONIoT”,“INFO”, string1)

-- retrieve the content of a URL
local rawname = url
local fname = filename  --rawname:match("[%w+.%w]+$")
local fulllocalpath = FTPATH .. fname

log("UMONIoT","INFO", "Starting download to local directory: " .. fulllocalpath)
local resultstr = "Starting download to local directory: " .. fulllocalpath .."\r" 
--local response, resultcode

local response_body = {}
local response, resultcode, respheaders, respstatus;


if rawname:match("https://") then
	local https = require'ssl.https'
	
	--response, resultcode = https.request(url) --TLZ commented out v1.4.6 5/9/23, replaced with following code to enable file transfer using tlsv1_2
	response, resultcode, respheaders, respstatus  =  https.request{
	--response, resultcode =  https.request{
		url = rawname,
		protocol = 'tlsv1_2',
		method = 'POST',
		headers = {
			[“content-type”] = “application/x-www-form-urlencoded”,
			[“content-length”] = string.len(sendString) --<---What is sendString????
		},
		
		sink = ltn12.sink.table(response_body),
		source = ltn12.sourcee.string(sendString)
	}
	--p(response, resultcode)
	log("UMONIoT","INFO", "File download response [" .. response .. "].")
	log("UMONIoT","INFO", "File download resultcode [" .. resultcode .. "].")
	--log("UMONIoT","INFO", "File download respheaders [" .. respheaders .. "].")  causes error
	log("UMONIoT","INFO", "File download repstatus [" .. respstatus .. "].")
	--log("UMONIoT","INFO", "File download response_body [" .. response_body .. "].")  causes error
else
	local http = require("socket.http")
	response, resultcode = http.request(url)
	--p(response, resultcode)
end

if resultcode ~= 200 then
	log("UMONIoT","ERROR", "Failed to download file. Error code [" .. resultcode .. "].")
	resultstr = resultstr .. "Failed to download file. Error code [" .. resultcode .. "]."
	return false, nil, nil, resultstr
else
	-- save the content to a file
	if isbinary == true then
		local f = assert(io.open(fulllocalpath, 'wb')) -- open in "binary" mode
		f:write(response)
		f:close()
	else
		local f = assert(io.open(fulllocalpath, 'w'))
		if fname:match("SETUP") then
			local newresponse = updateIPandCredentials(response)
			f:write(newresponse)
			f:close()
		else
			f:write(response)
			f:close()
		end
	end
	log("UMONIoT","INFO", "Successfully downloaded file: " .. fname .. " to gateway.")
	resultstr = resultstr .. "Successfully downloaded file: " .. fname .. " to gateway."
	return true, filename ,fulllocalpath, resultstr
end

end

Can you provide actual source code for this example please? For example, how to build sendString? The code shown above seems out of order. I am a true newbie.

Terry

sendString is just a local string variable that contains the data that is being sent to my server (url).
In my case this string is a concatenation of parameters read from the devicetree.
local sendString = ip…‘+’…power…‘+’…rssi…‘+’…latp…‘+’…lonp
I send this data from the gateway to a file on my server for processing.

The format I posted is a single function call using the format from the LuaSocket documentation. Look at the references at the bottom of my original post. I would suggest going through those to make sure you have the format correct.

HI Cory:

In my case I am asking dropbox to send me a file with all of the information included as part of the url download link: https://dropbox.com/xxxxxxxxxxxdl=1. Then would I need a sendString variable?
Also what would the content length then be?