Liz
Liz

Reputation: 1048

How to use cURL to download files from Box file picker URL

I am using the Box file picker in my web application to send a JS object of info. based upon the file that is chosen in the widget. In the object, part of what is returned is a URL that is valid for 15 minutes. This URL allows you to view and download the file. I am trying to utilize cURL to download the file but it doesn't seem to be working. I'm wondering if I'm missing a step in the process.

Here is a screenshot of the object that is returned successfully from the file picker widget: enter image description here

And here is my cURL attempt to download the file utilizing that same URL in the command line: enter image description here

Here is my code for the file picker widget (I've replaced the client ID with hashes):

 <div id="box-select" data-link-type="shared" data-multiselect="true" data-client-id="#########"></div>
 <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
 <script type="text/javascript" src="https://cdn01.boxcdn.net/js/static/select.js"></script>
 <script>
 $(document).ready(function() {
  var boxSelect = new BoxSelect();
  // Register a success callback handler
  boxSelect.success(function(response) {
    console.log(response);
  });
  // Register a cancel callback handler
  boxSelect.cancel(function() {
    console.log("The user clicked cancel or closed the popup");
  });
  });
  </script>

Please let me know if I'm missing something in the configuration to get this working. Thank you for the help!

Upvotes: 0

Views: 4897

Answers (2)

Abdul Vajid
Abdul Vajid

Reputation: 1411

You can use the browser hack of copy as curl. Below are the steps:

  1. Open the link from your browser and do a sample download through the UI. Keep the "Network tab" of browser dev tools open.

  2. Right click and do a "copy as curl" option for the "download" request that you see appearing in the network requests when you download it through the UI.

  3. Paste that curl on the terminal and you are done.

Note: Based on your curl version, you may see a warning by the curl program about risk of seeing binary response in terminal and causing undesired response on the curl. If you see the said warning, please use the --output filename.zip option along with curl

Upvotes: 1

M Kowalski
M Kowalski

Reputation: 460

You configured Box file picker to give you shared link type, which is a link accessible for other Box users, through the browser, depending on permissions.

If you want a download link that can be used by anyone (valid for 15 minutes) you need a direct link type. So change first line of you file picker code to data-link-type="direct", and you should get much longer URL, that you can use with curl.

https://developer.box.com/v2.0/docs/the-box-file-picker#section-configuration-options

Upvotes: 1

Related Questions