# Https.request simple and generic forms

**URL:** <https://forum.sierrawireless.com/t/https-request-simple-and-generic-forms/14363>\
**Category:** ALEOS Application Framework\
**Created:** [November 2, 2018, 7:09pm UTC](https://forum.sierrawireless.com/t/https-request-simple-and-generic-forms/14363 "2018-11-02T19:09:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CoryL](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@CoryL](https://forum.sierrawireless.com/u/CoryL)\
**Post date:** [November 2, 2018, 7:09pm UTC](https://forum.sierrawireless.com/t/https-request-simple-and-generic-forms/14363/1 "2018-11-02T19:09:38Z")

</div>

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](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

```auto
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](http://lua-users.org/wiki/FiltersSourcesAndSinks) based.

The generic form is now:

local res, code, headers, status = https.request({  
url = “[https://myserver/modem\_scripts/modemUpdate.php](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](http://w3.impa.br/~diego/software/luasocket/http.html)  
[LuaSocket LTN12 module](http://w3.impa.br/~diego/software/luasocket/old/luasocket-2.0-beta3/ltn12.html)

---

<div class="post-metadata">

**Author:** ![terryz](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@terryz](https://forum.sierrawireless.com/u/terryz)\
**Post date:** [May 16, 2023, 1:10pm UTC](https://forum.sierrawireless.com/t/https-request-simple-and-generic-forms/14363/2 "2023-05-16T13:10:23Z")

</div>

## I am trying to download a file from url = [https://dropbox.com/xxxxxxxxxxxdl=1](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.

---

<div class="post-metadata">

**Author:** ![CoryL](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@CoryL](https://forum.sierrawireless.com/u/CoryL)\
**Post date:** [May 16, 2023, 3:58pm UTC](https://forum.sierrawireless.com/t/https-request-simple-and-generic-forms/14363/3 "2023-05-16T15:58:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![terryz](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@terryz](https://forum.sierrawireless.com/u/terryz)\
**Post date:** [May 16, 2023, 7:59pm UTC](https://forum.sierrawireless.com/t/https-request-simple-and-generic-forms/14363/4 "2023-05-16T19:59:59Z")

</div>

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](https://dropbox.com/xxxxxxxxxxxdl=1). Then would I need a sendString variable?  
Also what would the content length then be?
