How to cfhttp post with binary file (.pdf)

Simple cfhttp. When I post to an api I am using

<cfhttp result="result" method="POST" url="http://localhost:5555/"  multipart="yes">
    <cfhttpparam type="FILE" name="new.pdf" file=tmp.pdf">
</cfhttp>
<cfdump var="#result#">

It returns:

**invalid variable declaration** [%PDF-1.4
%����
1 0 obj

So i tried to just read the file and push it to the browser to make sure It wasn’t the file.

<cffile action="read" tmp.pdf" variable="myFile">
<cfcontent type="application/pdf" variable="#myFile#">

It returns:

**invalid variable declaration** [%PDF-1.4
%����
1 0 obj

If I use action=“readbinary” instead of read it works as expected:

<cffile action="readbinary" tmp.pdf" variable="myFile">
<cfcontent type="application/pdf" variable="#myFile#">

So I am pretty sure that

<cfhttpparam type="FILE" name="new.pdf" file=tmp.pdf">

Is reading the file the same way as it would with cffile. How do i read a binary file in cfhttp / cfhttpparam type=“FILE” so it will work?

If I push the file with curl it works as expected:

curl "http://localhost:5555" -X POST -F "file=@/tmp.pdf"

Oddly enough this works. I’d rather not hav to use curl. So if you know what going on here. I would appreciate the help.

<cfexecute name = "/usr/local/bin/curl" 
arguments = ' curl "http://localhost:5555 -X POST -F "file=@/tmp.pdf"' 
variable="mypost"
timeout = "20"> 
</cfexecute> 

Thanks,

I’m successfully using cfhttp with cfhttpparam to post image files to an API. The only difference I can see with your code is that I’m using an absolute path to the image in my cfhttpparam file attribute. But the error you’re getting does suggest a problem with the PDF rather than its path.

It might just be a pdf thing. If anyone is curious here is the raw sent to postman:

I notice: content-length: 66786 is different.
Lucee adds: Content-Transfer-Encoding: binary (I don know how to remove that to see if thats the issue )
Curl boundary is different: boundary=------------------------ (I don know how to change that to see if thats the issue )

I’m posting to an external API so testing there is out of my control. If I had to guess I would think the external API I am hitting is expecting boundary=------------------------ .

It would be cool if these headers could be customized in cfhttp.

Using CURL:

POST /crm/v2/Deals/2789559000004194511/Attachments HTTP/1.1
accept: */*
authorization: 15745d801546c00ae3554fe03523533a
content-length: 66786
content-type: multipart/form-data; boundary=------------------------0f5f07c1fa02dd16
expect: 100-continue
host: localhost:5555
user-agent: curl/7.59.0
Cache-Control: no-cache
Postman-Token: e64a87c1-ec16-99f5-54fb-979ec5bc4995

--------------------------0f5f07c1fa02dd16
Content-Disposition: form-data; name="file"; filename="tmp.pdf"
Content-Type: application/pdf

%PDF-1.4
%����
1 0 obj
<</Subtype/Form/Filter/FlateDecode/Type/XObject/Matrix[1 0 0 1 0 0]/FormType 1/Resources<</ProcSet[/PDF/Text/ImageB/ImageC/ImageI]/ExtGState<</GS1 2 0 R>>/Font<</F1 3 0 R/F2 4 0 R/F3 5 0 R>>/XObject<</img0 6 0 R>>>>/Length 1828/BBox[0 0 595.42 841.69]>>stream


using LUCEE cfhttp

POST /crm/v2/Deals/2789559000004194511/Attachments?newFormat=1 HTTP/1.1
accept: */*
accept-encoding: gzip
authorization: 15745d801546c00ae3554fe03523533a
connection: Keep-Alive
content-length: 66808
content-type: multipart/form-data; boundary=vi5aQulOKi0OmQOefhCZUkd4-e93R6JM
host: localhost:5555
user-agent: Lucee (CFML Engine)
Cache-Control: no-cache
Postman-Token: d81a493d-b3a4-858e-b698-c5dbbf604ab0

--vi5aQulOKi0OmQOefhCZUkd4-e93R6JM
Content-Disposition: form-data; name="new.pdf"; filename="tmp.pdf"
Content-Type: application/pdf
Content-Transfer-Encoding: binary

%PDF-1.4
%����
1 0 obj
<</Subtype/Form/Filter/FlateDecode/Type/XObject/Matrix[1 0 0 1 0 0]/FormType 1/Resources<</ProcSet[/PDF/Text/ImageB/ImageC/ImageI]/ExtGState<</GS1 2 0 R>>/Font<</F1 3 0 R/F2 4 0 R/F3 5 0 R>>/XObject<</img0 6 0 R>>>>/Length 1828/BBox[0 0 595.42 841.69]>>stream