Linx Main Linx Help

Downloading files from APIs

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 PlaceGOAT 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 http://placegoat.com/goatse/200/200 endpoint.

This will return a 200x200 image of a goat in the form of a file stream.

The file stream response is then written to a local folder location.

Sample:

A sample Solution is available below for a practical demonstration .

DownloadingFiles.lsoz (4.3 KB)

To write a file from a file stream:

  1. Set the URL to https://placegoat.com/goatse/200/200.

    If you debug the Process, you will notice the Response Body contains a bunch of weird characters like below:

    ���� 
    

    You need to convert this into a List<Byte>.

  2. Configure the rest.callrestendpoint Output type to be List<Byte>.

    When you debug your Process, the Response Body should be deserialized into a ListTYP object:
    image

  3. Add a BinaryFileWriteFNC to the Process.

    This allows writing binary content as a file.

  4. Configure the BinaryFileWrite to have a File path of a location you want i.e. C:/Linx/goatpic.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.
  5. Reference the CallRESTEndpoint.ResponseBody as the Contents

  6. Debug the Process , once finished, take a look at the file location specified in Step 4.
    image

Note:

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.