Expand my Community achievements bar.

SOLVED

Posting FormData object to external API from Workflow

Avatar

Level 2

Hello,

I would like to know if anyone can help me find how to send a file to an external API.

My external API has a "Content-Type = multipart/form-data" requirement.

As far as i know the only way to do this in javascript normally is to declare a FormData object, append values and file to it and send it through XmlHTTPRequest using Post method.

Problem is, the FormData object is not defined in Adobe Campaign Classic JS so I'm stuck. 

 

Thank you.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 2
2 Replies

Avatar

Correct answer by
Level 2

Avatar

Level 1

To expand on this solution for less-technical people like me...

 

I needed to send a simple csv file that contained a single column with column header and the file needed to be called upFile. I used a workflow to with a Data Extraction to create the file in the previous step and then used the following in a js node

 

logInfo("File name is: " + vars.filename)
memBuf = new MemoryBuffer()
memBuf.load(vars.filename)
data = memBuf.toString()

boundary = "nVenJ7H4puv"  //can be anything you want

body = "--" + boundary + '\r\n'  //first boundary needs an extra -- in front
body = body + "Content-Disposition: form-data; name='upFile'; \r\n"    //upFile was the name I needed to give the file to be uploaded
body = body + "Content-Type: text/csv\r\n\r\n" //two carriage returns required before adding the data
body = body + data + '\r\n'
body = body + "--" + boundary + "--\r\n"   //last boundary needs to have -- appended at start and end 

 

url ="<url of endpoint>"

var http=new HttpClientRequest(url);
http.header ["Content-Type"] = "multipart/form-data; boundary=" + boundary;
http.header ["Accept"] = "*/*";
http.header ["Authorization"] = "<whatever you need>";
http.method = "POST" ;
http.body = body;

http.execute();

//Get the response in string format.
var result = http.response.body.toString();
logInfo("Result String: "+result);
var responseCode = http.response.code
logInfo("Response code is: "+ responseCode)