We Want to Hear From You! What Do You Want to See on the Community? Tell us here!

Forum Discussion

bryancart32's avatar
bryancart32
Explorer | Level 3
8 years ago

Help downloading a non-image file using the API

I am trying to use the v2 API to to get a temporary link to a file.  JPG files work fine, but I have some .dcm files.  These are medical image DICOM format files.  I need to be able to create a temporary link to this file so that I can download it from my own web application.

I am calling the API with this message:

{"path":"/DICOM/13c76bf2914c6d87786d555f0994c531/1.dcm"}

I am getting this result: Invalid URL

I know the URL is good and I can see the file in my account.

Can you please let me know how to do what I am trying to do?

Thanks!
Bryan Whitaker

10 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    Hi Bryan, to clarify, are you calling /2/files/get_temporary_link with that parameter? That shouldn't produce an error like that. Please share the full request and response (just be sure to redact the access token) so we can take a look. Thanks!
  • bryancart32's avatar
    bryancart32
    Explorer | Level 3
    8 years ago

    Thank you for your reply.

     

    When I log into dropbox, this is the URL for the file preview:

    https://www.dropbox.com/home/DICOM/13c76bf2914c6d87786d555f0994c531?preview=1.dcm

     

    Here is the call I am making to the API:

     

    URL: https://api.dropboxapi.com/2/files/get_temporary_link

    Auth: Bearer TOKEN

    JSON Post:

       {"path":"/DICOM/13c76bf2914c6d87786d555f0994c531/1.dcm"}

    Response:

       Invalid URL: https://api.dropboxapi.com/2/files/get_temporary_link

     

    I think that's it.  Your help is greatly appreciated.

    Bryan

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    Thanks Bryan. That is the correct URL for this API endpoint, and that doesn't seem to be the actual API request/response though.

     

    For example, using curl, this works for me: (just using a made up 1.dcm file in my test account)

     

    $ curl -vX POST https://api.dropboxapi.com/2/files/get_temporary_link \
    >     --header "Authorization: Bearer <ACCESS_TOKEN>" \
    >     --header "Content-Type: application/json" \
    >     --data "{\"path\":\"/DICOM/13c76bf2914c6d87786d555f0994c531/1.dcm\"}"
    
    > POST /2/files/get_temporary_link HTTP/1.1
    > User-Agent: curl/7.33.0
    > Host: api.dropboxapi.com
    > Accept: */*
    > Authorization: Bearer <ACCESS_TOKEN>
    > Content-Type: application/json
    > Content-Length: 56
    >
    } [data not shown]
    * upload completely sent off: 56 out of 56 bytes
    < HTTP/1.1 200 OK
    * Server nginx is not blacklisted
    < Server: nginx
    < Date: Thu, 27 Jul 2017 23:19:17 GMT
    < Content-Type: application/json
    < Transfer-Encoding: chunked
    < Connection: keep-alive
    < Vary: Accept-Encoding
    < Vary: Accept-Encoding
    < Cache-Control: no-cache
    < Pragma: no-cache
    < X-Content-Type-Options: nosniff
    < X-Dropbox-Http-Protocol: None
    < X-Dropbox-Request-Id: 388c65354c4b571dbcc5ce3e25d736f0
    < X-Frame-Options: SAMEORIGIN
    < X-Server-Response-Time: 315
    <
    { [data not shown]
    100   811    0   755  100    56    835     61 --:--:-- --:--:-- --:--:--   940
    * Connection #0 to host api.dropboxapi.com left intact
    {
      "metadata": {
        "name": "1.dcm",
        "path_lower": "/dicom/13c76bf2914c6d87786d555f0994c531/1.dcm",
        "path_display": "/DICOM/13c76bf2914c6d87786d555f0994c531/1.dcm",
        "id": "id:25N5ksooX-sAAAAAAAMc8Q",
        "client_modified": "2017-07-27T21:46:04Z",
        "server_modified": "2017-07-27T21:46:05Z",
        "rev": "71d18021eccc7",
        "size": 9,
        "content_hash": "824979ede959fefe53082bc14502f8bf041d53997ffb65cbbe3ade5803f7fb76"
      },
      "link": "https://dl.dropboxusercontent.com/apitl/1/AACh0gTVcnX-KmJkXbSH5qALdmLo1QoLJyQHbpVRXKVYBbn2ZFaJsIXdAXr1JM0sTg7RiQPaeymbJmyeSpkDaA505w_9tl_W6rGoXFr90T-1eMy9A3KGzfhnTORWSFbRi9JTPFJInNZepU2rZp6EzuLbjC64SXHpeVnT2F35auCpVQjDepr3hjxBudK1tF2uLmcpk5LhylRJGOpcCZrlT0hJlowU0huvfaUCqX-6ZgAiVsPDuuOVGSs-bOfNaj-LvnImgmkkG_e0fxusqFjpVu6MH0roCfouL3QQI60Izt8cFg"
    }

     

    What are you using to make the call? Can you share a screenshot showing it? Thanks in advance! 

  • bryancart32's avatar
    bryancart32
    Explorer | Level 3
    8 years ago

    I used PHP and fopen.  This is my function:

    function do_post_request($url, $data, $optional_headers = null)  {
    
       $params = array('http' => array('method' => 'POST','content' => $data));
       if ($optional_headers !== null) { $params['http']['header'] = $optional_headers; }
       
       echo "<pre>";print_r($params);echo "</pre>";
       
       $ctx = stream_context_create($params);
       try {
          $fp = @fopen($url, 'rb', false, $ctx);
       } catch(Exception $ex) {
          echo '1 Error: ' .$ex->getMessage();
       }
       //if (!$fp) { throw new Exception("Problem with $url, $php_errormsg"); }
       if (!$fp) { echo "2 Error: ".$php_errormsg; }
       $response = @stream_get_contents($fp);
       echo "<pre>";print_r($response);echo "</pre>";
    
       if ($response === false) { $response = "Invalid URL: ".$url;/*echo "Problem reading data from $url.";*/ }
    
       return $response;
    }


    THIS ONE WORKED DOWNLOADING A JPG FILE:

     

    Array
    (
        [http] => Array
            (
                [method] => POST
                [content] => {"path":"/DICOM/13c76bf2914c6d87786d555f0994c531/1.jpg"}
                [header] => Content-Type: application/json
    Authorization: Bearer [TOKEN-HERE]
    
            )
    )


    RETURNED:

     

    {
       "metadata":{
          "name":"1.jpg",
          "path_lower":"/dicom/13c76bf2914c6d87786d555f0994c531/1.jpg",
          "path_display":"/DICOM/13c76bf2914c6d87786d555f0994c531/1.jpg",
          "id":"id:PnjeM0FAI_AAAAAAAAAJWQ",
          "client_modified":"2017-07-07T14:26:41Z",
          "server_modified":"2017-07-07T14:26:41Z",
          "rev":"13004a8335bf",
          "size":982513,
          "content_hash":"3a3179223b1b08759a3b5d222d1a7d226b16557b3f98b75d88874bce27d74bb2"
       },
       "link":"https://dl.dropboxusercontent.com/apitl/1/AACiQGW0Md8EYbPrqqNkdPsl ..."
    }


    THIS ONE FAILED DOWNLOADING A DCM FILE:

     

    Array
    (
        [http] => Array
            (
                [method] => POST
                [content] => {"path":"/DICOM/13c76bf2914c6d87786d555f0994c531/1.dcm"}
                [header] => Content-Type: application/json
    Authorization: Bearer [TOKEN-HERE]
    
            )
    
    )


    I do not know how to get any more details of why the response failed.  

    Any other ideas?

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    Thanks! That's helpful. So, the "Invalid URL" error isn't coming from the API itself, but rather just from this line of code:

     

     

    if ($response === false) { $response = "Invalid URL: ".$url;/*echo "Problem reading data from $url.";*/ }

    That will occur if `$response` is false, and `$response` will be false based on `@stream_get_contents($fp)`. The `$fp` comes from `fopen`, which will return false if an error occurs.

     

    So, if something went wrong with the API call itself, `fopen` would return false, which wouldn't be a valid `resource` as `stream_get_contents` expects,  so `stream_get_contents` would then return false, and trigger your `$response === false` line.

     

    According to the fopen documentation: "If the open fails, an error of level E_WARNING is generated.". I can't offer much general PHP advice, but it looks like there is some information in this StackOverflow post about getting the error information, if that's where the issue is occuring.

     

    It looks like you already have some other `echo` statements in the case where things failed though. Are you getting any output from those?

     

     

    Alternatively, you can use curl in PHP instead, like shown in this example.

  • bryancart32's avatar
    bryancart32
    Explorer | Level 3
    8 years ago

    I finally was able to find that the error I am getting back from DropBox is this:

     

       fopen(https://api.dropboxapi.com/2/files/get_temporary_link): failed to open stream: HTTP request failed! HTTP/1.1 409 Conflict

     

    I did some searching and I was not able to see why this is happening.  Any ideas?

     

    Thanks!

    Bryan

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    Thanks! That "HTTP/1.1 409 Conflict" is part of the API response, but it's only the status line. Can you print out the rest? The response body should contain a specific error.
  • bryancart32's avatar
    bryancart32
    Explorer | Level 3
    8 years ago

    409 - Conflict
    {"error_summary": "path/not_found/..", "error": {".tag": "path", "path": {".tag": "not_found"}}}

     

    When I look in the folder: /DICOM/13c76bf2914c6d87786d555f0994c531

     

    I have 4 files:  1.jpg, 1.dcm, 2.jpg, 2.dcm

     

    The exact same code works for 1.jpg and 2.jpg, but fails for 1.dcm and 2.dcm.

     

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago

    Thanks! That is the response we're looking for. 

     

    The /2/files/get_temporary_link documentation covers what the 'not_found' error means. In this case, it does mean there was nothing found at the path supplied in the 'path' parameter. 

     

    How are you checking that the folder does contain 1.dcm and 2.dcm? Be careful checking via a computer, for example, in case your folder isn't syncing (e.g., maybe your desktop client isn't running) or in case it's linked to a different account.

     

    You can use /2/files/list_folder to list the contents of the folder, and /2/users/get_current_account to check the linked account.

  • bryancart32's avatar
    bryancart32
    Explorer | Level 3
    8 years ago

    Thank you so much for your help.  I feel like an idiot, but I have two different DropBox accounts for testing and I was using the wrong token and once I corrected that it's working great.

     

    Thanks again!

    Bryan

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,033 PostsLatest Activity: 3 years ago
409 Following

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!