Search code examples
coldfusioncoldfusion-10coldfusion-11

How to get the filename from a cfhttp get request?


If I run the following code to get an image from Medium's site:

<cfhttp url="https://cdn-images-1.medium.com/max/600/1*3j1McX-y1rvKewzI2gWc_w.png" 
method="get" useragent="#CGI.http_user_agent#" getasbinary="yes">

I then want to save the image with the same name that they used i.e. 1*3j1McX-y1rvKewzI2gWc_w.png.

How can I get the name of the file from the cfhttp request? I looked in the cfhttp.header for any sign of the content-disposition attribute but can't find it.


Solution

  • Assuming you are getting these URLs dynamically, why not just parse it for the filename first, then apply that to the filename attribute?

    <cfset filename1 = ListLast("https://cdn-images-1.medium.com/max/600/1*3j1McX-y1rvKewzI2gWc_w.png","/") />
    
    <cfhttp url="https://cdn-images-1.medium.com/max/600/1*3j1McX-y1rvKewzI2gWc_w.png" 
    method="get" useragent="#CGI.http_user_agent#" getasbinary="yes" path="whateverpath" filename="#filename1#>