5

I'm trying to download a file from Box.com through API using the following code.

<cfhttp url="https://api.box.com/2.0/files/(FILE_ID)/content/" method="GET" redirect="true" >
<cfhttpparam type="header" name="Authorization" value="Bearer (DEVELOPER_TOKEN)">
</cfhttp>

As per documentation it should return 302 Found as response. And redirects to dl.boxcloud.com for download. But I'm getting 200 as response.

enter image description here

Not sure why I'm getting 200 as response. I need to download the file through the API call. Did I missed anything?

Rajesh Manilal
  • 1,104
  • 9
  • 20
  • 2
    A "200 OK" is a good response code. Looking at your dump it looks like the image you requested `test.jpg` is being streamed to you with content type `image/jpeg` and content length `12681`. The browser would normally handle that response for you and show the image (or offer to download). Since you are calling this service yourself you will need to handle the response appropriately. – Miguel-F Apr 06 '17 at 12:50

1 Answers1

3

With respect to @Miguel-F's comment, I've surfed and found a solution from Ben Nadel's post.

I've got 200 as response, this is because ColdFusion followed the redirect to dl.boxcloud.com (since by default, the REDIRECT attribute is TRUE), and the redirected request's response is 200.

Actually we should stop the redirect by setting REDIRECT attribute to FALSE. So that Coldfusion will return actual response to the invoking code.

So I've set REDIRECT attribute to FALSE.

<cfhttp url="https://api.box.com/2.0/files/(FILE_ID)/content/" method="GET" redirect="false" >
<cfhttpparam type="header" name="Authorization" value="Bearer (DEVELOPER_TOKEN)">
</cfhttp>

And now I'm getting 302 found as response as per the documentation.

enter image description here

With this response, we're having Location key (in which the code was redirected earlier) in the ResponseHeader. So by using the Location URL we can download the file using CFHEADER and CFCONTENT tags.

Reference : https://www.bennadel.com/blog/934-ask-ben-handling-redirects-with-coldfusion-cfhttp.htm

Rajesh Manilal
  • 1,104
  • 9
  • 20
  • 1
    Great! Glad you got this working and came back here to document the solution. Remember to mark it as the answer when you can. – Miguel-F Apr 07 '17 at 12:28