Download file content?
Downloading file content from a REST API involves submitting a request which would then return a file as a file stream
or binary
content. This data is then translated into a ListTYP of the type ByteTYP ( List<byte>
) in Linx. The List<byte>
must then be written to a directory using a BinaryFileWriteFNC .
To demonstrate the approach, an example request is made to the cat API. This API returns placeholder images of goats (this approach will walk for all file types). This is a free and open API so you can try it for yourself.
In the below example, a GET
request is made to the https://http.cat/401
endpoint.
This will return image of a cat in the form of a file stream
.
The file stream
response is then written to a local folder location.
A sample Solution is available below for a practical demonstration .
Linx 5.22.0 DownloadingFiles.lsoz (4.3 KB)
Linx 6.0.5 Linx6.zip (3.0 KB)
To write a file from a file stream:
-
Set the URL to
https://http.cat/401
.If you debug the Function, you will notice the
Response Body
contains a bunch of weird characters like below:����
You need to convert this into a
List<Byte>
. -
Configure the rest.callrestendpoint Output type to be
List<Byte>
.When you debug your Function, the
Response Body
should be deserialized into a ListTYP object:
-
Add a BinaryFileWriteFNC to the Function.
This allows writing binary content as a file.
-
Configure the BinaryFileWrite to have a File path of a location you want i.e.
C:/Linx/cat.jpeg
Note:
You can give the file any name you want but the extension must match the file type being downloaded (i.e. ".jpeg") or else you will have a corrupted file. -
Reference the
CallRESTEndpoint.ResponseBody
as the Contents -
Debug the Function , once finished, take a look at the file location specified in Step 4.
If the File exists is left as the default
Append data
, then the file data will be appended and will be corrupted. In order to Overwrite
or Increment file name
in cases where the file already exists, select the appropriate option.