Question
· Apr 17, 2023

How to Download files using %Net.HttpRequest

I am using the %Net.HttpRequest class to download a file from the internet .using the following code. When a file is returned in the DOM I am able to read the file content and save it to the desired location but when my response is a download dialog nothing is returned in the HttpResponse. Am I doing something wrong here is where I have gotten to so far any help appreciated.

current code

Set httpRequest = ##class(%Net.HttpRequest).%New() Set httpRequest.Server = "msedgedriver.azureedge.net"
set httpRequest.Port=443
Set httpRequest.ContentType = "application/octet-stream"
Do httpRequest.Get("/113.0.1774.0/edgedriver_win64.zip")
    
    Do $System.OBJ.Dump(httpRequest.HttpResponse)
    
    Set fileName = "tryedgedriver_win64.zip"    ;; specify the desired file name
    Set fileStream = ##class(%Stream.FileBinary).%New()
    Set fileStream.Filename = "C:\"_fileName
    Do fileStream.CopyFrom(httpRequest.HttpResponse.Data)
    Do fileStream.%Save()
    Do fileStream.%Close()

 

Dump returns:

+----------------- general information ---------------
|      oref value: 2
|      class name: %Net.HttpResponse
| reference count: 1
+----------------- attribute values ------------------
|    ContentBoundary = ""
|        ContentInfo = ""
|      ContentLength = ""
|        ContentType = ""
|               Data = "1@%Stream.GlobalBinary"
|        HttpVersion = ""
|       ReasonPhrase = ""
|         StatusCode = ""
|         StatusLine = ""
+-----------------------------------------------------

the file is written to the folder but with nothing in it

Product version: Ensemble 2018.1
$ZV: Cache for Windows (x86-64) 2018.1.4 (Build 505_1U) Thu May 28 2020 10:01:40 EDT [HealthShare Modules:Core:15.032.9035 + Linkage Engine:15.032.9035]
Discussion (1)1
Log in or sign up to continue

Hi Thembelani,

This site requires access by HTTPS.

I created a TLS configuration in Management Portal with defaults called "Open".

Then the following works for download.

Set httpRequest = ##class(%Net.HttpRequest).%New() Set httpRequest.Server = "msedgedriver.azureedge.net"
set httpRequest.Port=443
Set httpRequest.ContentType = "application/octet-stream"
Set httpRequest.SSLConfiguration="Open"
set httpRequest.Https=1
Do httpRequest.Get("/113.0.1774.0/edgedriver_win64.zip")
Set fileName = "tryedgedriver_win64.zip"
Set fileStream = ##class(%Stream.FileBinary).%New()
Set fileStream.Filename = "C:\tmp\"_fileName
Do fileStream.CopyFrom(httpRequest.HttpResponse.Data)
Do fileStream.%Save()
Do fileStream.%Close()

Kind regards,

Alex