<cfhttp> to download a remote .csv file in ColdFusion

854 Views Asked by At

I'm trying to download a .csv file on a remote server location using ColdFusion (version 2016). I used the cfhttp tag to perform this operation but I keep getting the following error:

401 UNAUTHORIZED

I checked with the server admin of the remote server to verify that I have the right domain name, userid and password and the admin confirmed it. I couldn't find anything similar on SO, hence posting this question. Hoping someone can help me out.

PS: I don't have access to the ColdFusion Administrator since it is hosted by a separate team.

Below is my code (actual values replaced with dummy data for security):

  <cfhttp  url="https://xxx.yyy.com/abcd/xyz/myfolder/myFile.csv">
    <cfhttpparam type="header" name="Authorization"  
        value="Basic #ToBase64("MydomainName\Myuserid:Mypassword")#" />
  </cfhttp>
  <cfdump  var="#cfhttp.filecontent#" label="CSV file content">
  <cfabort>
1

There are 1 best solutions below

5
On

It might be that your auth string is wrong. To avoid confusion and while debugging, I would suggest something like this:


<cfset myDomainName = "MydomainName" />
<cfset myUserId = "Myuserid" />
<cfset myPassword = "Mypassword" />

<cfset authString = "Basic " & ToBase64('#myDomainName#' & '\' & '#myUserId#' & ':' & '#myPassword#') />

<cfhttp  url="https://xxx.yyy.com/abcd/xyz/myfolder/myFile.csv">

  <cfhttpparam type="header" name="Authorization"  
        value="#authString#" />

</cfhttp>

<cfdump  var="#cfhttp.filecontent#" label="CSV file content">
<cfabort>

I hope this helps.