cancel
Showing results for 
Search instead for 
Did you mean: 

How to download a file using web api.

AZ
Level 3
I want to download a file using Web API. After making request we are getting File data as text in the response content data item.

For your reference I am getting the response as below as text type.

%PDF-1.4 %����
68 0 obj <</Linearized 1/L 1850803/O 70/E 262535/N 13/T 1849323/H [ 916 320]>> endobj
xref
68 31
0000000016 00000 n
0000001236 00000 n
0000001456 00000 n
0000001821 00000 n
0000001957 00000 n
0000002087 00000 n
0000002229 00000 n
0000002972 00000 n
0000003602 00000 n
0000004428 00000 n
0000005000 00000 n
0000005715 00000 n

I am not adding all the content here.

How can we save this content as a original file.

Please help, its an urgent.

Thanks
K

------------------------------
A Z
------------------------------
10 REPLIES 10

ewilson
Staff
Staff
Unfortunately, binary streams are not supported as a return type of a Web API service. They only return string data. This has been raised with product as a limitation of the feature and should be addressed in a future release.

In the meantime, I would suggest using a code stage and something like the .NET HttpClient class to retrieve your file.

Cheers,

------------------------------
Eric Wilson
Director, Partner Integrations for Digital Exchange
Blue Prism
------------------------------

Hi Eric and Blue Prism support team,

I am trying to download PDF file in Box using BP Web API.

Does the Web API limitation still persists in BP 6.4? or it has been fixed already.
Please, can you provide instruction on where to add the .Net Code in Web API and how to handle the Response.

var client = new RestClient("https://api.box.com/2.0/files/71*****************/content");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer *********************");
request.AddHeader("Cookie", "box_visitor_id=*******************");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Thanks,
Michael



------------------------------
Michael Ng
------------------------------

Hi ​Michael,

Box supports several different integration options. The two that probably make the most sense for Blue Prism are their REST interface and their .NET SDK. Since you're wanting to download binary content from Box, a PDF in this example, you won't be able to use the Web API feature of Blue Prism. However, you can still integrate with Box's REST API using a new version of the HttpClient VBO, or you can use Code stages if you want to work through their .NET SDK.

Give me a few minutes and I will post the updated HttpClient VBO which now includes a method called "Http Request File". With this you'll be able to download a PDF and write it to disk.

Cheers,

------------------------------
Eric Wilson
Director, Partner Integrations for Digital Exchange
Blue Prism
------------------------------

Michael,

See the attached VBO. There is a new method called 'HTTP Request File'. You can use that to download your PDF from Box. This is also being added to the DX.

Here's what I believe the set up for the 'HTTP Request File' call would look like in your scenario:

16618.png

The Collection called 'CustomHeaders' holds the definition of the Cookie header in your example:

16619.png

Cheers,

------------------------------
Eric Wilson
Director, Partner Integrations for Digital Exchange
Blue Prism
------------------------------

Thank you Eric for the HTTP Request File Object.
I was able to download file using your object on REST API with Basic Authentication.
However, I had to make a small code change in your code to connect to Box Integration (Oauth 2.0).
Bearer token is only good for one hour. Need to figure it a way to generate new Bearer token using client_id and client_secrete every request.
Box provided javascript code to generate new token (Find attached) Now, I need to convert it from javascript to VB and incorporate it to Request File Object.   https://developer.box.com/guides/tooling/postman/quick-start/.

Try
Dim request As WebRequest = WebRequest.Create(addressURL)
If forcePreAuth Then
'Sometimes a web server will require the authorisation header in the initial request
'In which case we have to add the basic authorization header manually.
Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(String.Format("{0}:{1}",username,password))
Dim base64 As String = Convert.ToBase64String(bytes)

'For Basic Authorization
'request.Headers.Add("Authorization", "Basic " & base64)

' Box doesn't support Basic Authentication. 
'Have to use Oauth2, Bearer token is only good for one hour. Need to figure it a way to generate new Bearer token using client_id and client_secrete every request
request.Headers.Add("Authorization", ": Bearer *token************************************")


------------------------------
Michael Ng
------------------------------

Ah, that example is from a Postman collection for Box, so it might be a little difficult to translate to VB because of some of the Postman-specific objects. If you have trouble with it, let me know and I'll spend some time looking it over.

Cheers,

------------------------------
Eric Wilson
Director, Partner Integrations for Digital Exchange
Blue Prism
------------------------------

Hi,
I have a similar case. I am trying to download a (pdf) file using this object, but authorization is via baerar token.
how can i use this object? possibly is there any ready-made solution?


------------------------------
Marcin Grzelak
RPA Developer
Open Life TU Życie S.A.
Europe/Warsaw
------------------------------

@Marcin Grzelak are you talking about plain bearer token where you already have the token (aka API key) and it just needs to be added to an authentication header? Or are you referring to something like OAuth2 JWT?

Cheers,


------------------------------
Eric Wilson
Director, Partner Integrations for Digital Exchange
Blue Prism
------------------------------

yes, I mean about plain bearer token. I already have the token.

------------------------------
Marcin Grzelak
RPA Developer
Open Life TU Życie S.A.
Europe/Warsaw
------------------------------