2

Quite recently, I was working with a third party REST API to which I was to POST a multipart/form-data consisting of two parts - one with a json object and the other with a pdf blob. The format of the body was as below:

--boundary
Content-Type: application/vnd.xx+json; charset=utf-8
Content-Disposition: form-data; name=data

{JSON-OBJECT}
--boundary
Content-Type: application/bin
Content-Disposition: form-data; name=bin; filename=Doc2.pdf; filename*=utf-8''Doc2.pdf 

***PDF-BLOB***
--boundary--

I have searched for days, but could not find a way to make this call happen using Coldfusion. I finally ended up implementing what was suggested here successfully: https://www.bennadel.com/blog/2252-apparently-coldfusion-cannot-handle-chunked-multi-part-form-data.htm

However, I am left wondering about the below:

  • Given that the above post is relatively old (~2011), does it still stand true today?
  • Can this not be done by making use of CFHTTP tag to build a chunked multipart form consisting of two parts each with a unique Content-Type and Content-Disposition?

Just for the records, here is what I have tried but haven't been very successful. Also want to note that, to me it does not intuitively make sense to set multiple Content-Type/Disposition within one cfhttp code block, or atleast I dont fully understand how that would be collated into one multipart/form-data request. However, when I was playing with the code I was trying everything I could to make it work.

<cfhttp url="#url#" method="POST" username="#userName#" password="#password#" multipart="true">
    <cfhttpparam type="header" name="Accept" value="*/*">
    <cfhttpparam type="header" name="Content-Type" value="application/vnd.xx+json; charset=utf-8">
    <cfhttpparam type="header" name="Content-Disposition" value="form-data; name=data">
    <cfhttpparam type="formfield" name="data" value="#body#">
    <cfhttpparam type="header" name="Content-Type" value="application/bin">
    <cfhttpparam type="header" name="Content-Disposition" value="form-data; name=bin; filename=#filename#; filename*=utf-8''#filename#">
    <cfhttpparam type="file" name="bin" file="#session.target#">
</cfhttp>
  • 1
    If you need to go into detail, don't bother with CF's "wrappers". Instead use what Java offers, something like Apache's `HttpClient` with `MultipartEntityBuilder` for example. Then utilize `createObject()` to work with that within CF. – Alex Jan 30 '20 at 01:40

0 Answers0