Solved

Downloading a file through an API

  • 28 June 2023
  • 6 replies
  • 169 views

Badge +1

Hi there,

 

We would like to know if its possible to download a pdf file (or convert it to base64) through an api. Sadly the API only gives the PDF file instead of a base64 encoded string.

 

If abose is possible. Please explain how. Because we dont know where to start even.

 

Results :

Preferred method : Save it as base64

Optional and not preferred : Save the file on our own server.

Must have : Displayable on our own application

icon

Best answer by Anthony 5 July 2023, 12:19

View original

This topic has been closed for comments

6 replies

Badge +1

Let me clarify some more. The response of that API gives an octet-stream (lots of binary code and in more precise words. a Byte stream)

Userlevel 7
Badge +5

Hi Anthony,

You can use an HTTP connector process action to retrieve the PDF file and use a subsequent Download file process action to let the user download the file.

If you want to store the file in the meanwhile, you can use a Write File connector.

Converting the file to base64 can be done via logic.

-- These are process flow parameters:
declare @binary_file varbinary(max) = 0xC290CAB59C869D41AB86DDB886204EF4
declare @base64_value varchar(max)

select @base64_value = base64_value
from openjson(
(
select base64_value
from (select @binary_file as base64_value) T
for json auto
)
) with (
base64_value varchar(max)
)

-- Clear the binary file
set @binary_file = null

-- The variable is now converted
select @binary_file, @base64_value

I hope this helps.

Badge +1

Allright, I`m gonna give this a try. Trying to keep you posted

Badge +1

Right, so a new issue has occured. Whenever I am calling to that API. It gives me nothing back. And one of the probable causes might be the headers.

 

Error: 

Process action "buildsimple_api_fetch_document_download" in processflow "api_get_builsimple_purchase_invoice_details" in application 99 returned the following message: "Unable to parse json for \"headers\" input parameter.
Error: After parsing a value an unexpected character was encountered: \". Path '[1].Key', line 8, position 0." (86be518d)

 

This is what we pass in our HTTP connector header

[

    {

        "Key": "X-Tenant-Id",

        "Value": "X"

    },

    {

        "Key": "Authorization",

        "Value": "X"

    },

    {

        "Key": "Accept",

        "Value": "application/octet-stream"

    }

]

 

Some crucial information has been replaced by X of course. But the authorization works as we tested this with also HTTP connectors.

 

Badge +1

Never mind regarding the above. I managed to fixed the headers. Which was not the problem. Im do now have a new challange.

 

I`m currently using postman to see the result of converting a binary/octet-stream to base64 and this is where my problem is starting.

 

The result of pastman base64 character count is approx ~30.000+ characters while the code I am using above can only hold up to 4000 characters. How do I solve this issue?

Badge +1

Never mind. I fixed the whole issue. Just inserting the binary into a binary column was the trick.

 

Thanks anyways