0

I am trying to make a request to an external API with SSJS and I have found this post where Eliot Harper posted a solution.

I have made a lot of API calls using SSJS but with Content-Type JSON. Now I need to make a new call with multipart/form-data and I am not able to do so as I don't know how the postdata atribute should be built.

I have the following request and the API always returns the same error (like if every field was missing).

var req = new Script.Util.HttpRequest(endpoint);
  req.emptyContentHandling = 0;
  req.retries = 0;
  req.continueOnError = true;
  req.contentType = "multipart/form-data; boundary=Test";
  req.method = "POST";
  req.setHeader("Customheader", "X");
  req.setHeader("Cache-Control", "no-cache");
  
  req.postData = "id=123";
  
  var resp = req.send();

I need to send more than one attribute but just with one, the error I receive is like I was not sending that attribute.

I have tested Eliot's solution but I only get a 400 error.

  <script runat="server">
    Platform.Load('core', '1.1');
    
    var url = 'xxxxxxxxx';
      
    var boundary = 'Test';
      
    var headerNames = ['Customheader', 'Cache-Control'];
    
    var headerValues = ['X', 'no-cache'];
    
    var contentType = 'multipart/form-data; boundary=Test';
    
    var result = [0];
    
    var payload = '';
    payload += '\r\n--' + boundary;
    payload += '\r\nContent-Disposition: form-data';
    payload += '\r\n\r\nid';
    payload += '\r\n123';
    payload += '\r\n--' + boundary + '--';
    
    try
    {
    var statusCode = Platform.Function.HTTPPost(url, contentType, payload, headerNames, headerValues, result);
    }catch(error)
    {
      Write(error.description)
    }

Does someone know how to make a call using SSJS with form-data content-type?

Thank you

3
  • 1
    Have you tried it in Postman? If not, I'd do that first. It's much easier to test and you can export functional JS code from Postman... then implement it with WSProxy. Mar 9, 2022 at 15:15
  • Hello Adam, I did try it first with postman and I was able to make the call, but I don't know how to build the body to be used in the postData attribute of the HTTPRequest.
    – Ruben
    Mar 9, 2022 at 16:37
  • There's a code tag on the right side of Postman </> and a drop-down. Set it to JavaScript and see what it generated for your code. Copy that as a starting place for your SSJS. Mar 9, 2022 at 17:36

1 Answer 1

1

After many tests with a colleague we have managed to see how the body of the request should be.

This link helped to understand this kind of requests.

Here is an example:

Content-Type: multipart/form-data; boundary=----------Test

String for the postData (body) of the request:

------------Test
Content-Disposition: form-data; name="field1"

xxxx
------------Test
Content-Disposition: form-data; name="field2"

xxxx
------------Test
Content-Disposition: form-data; name="field3"

xxxx
------------Test--

It is very restrictive with the line breaks and you have to be careful with the --, since it adds two at the beginning based on the content-type.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .