0

This is not about files, but about key-value pairs. And I already got below to work for ONE submission/record. How do I tweak below to support multiple record submissions?

private final static string BOUNDARY = '----sfdc-multi-form';

public static void submit(String access_token){
    String form_data = '';
    form_data += generateParam('mail_to_name', 'Captain America');
    form_data += generateParam('mail_to_address', '123 Main St');
    form_data += generateParam('mail_to_city', 'Phoenix');
    form_data += generateParam('mail_to_state', 'AZ');
    form_data += generateParam('mail_to_zip', '11001');
    form_data += EncodingUtil.convertToHex(Blob.valueOf('--' + BOUNDARY + '--'));

    Blob form_data_blob = EncodingUtil.convertFromHex(form_data);
    
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://example.com/add');
    request.setHeader('Connection', 'keep-alive');
    request.setHeader('Authorization', 'Bearer ' + access_token);
    request.setHeader('Content-Length', String.valueOf(form_data_blob.size()));
    request.setHeader('Content-Type', 'multipart/form-data; boundary="' + BOUNDARY + '"');
    request.setMethod('POST');
    request.setBodyAsBlob(form_data_blob);
    
    Http http = new Http();        
    HttpResponse response = http.send(request);
    system.debug(LoggingLevel.DEBUG, '----------> submit() RESPONSE BODY = ' + response.getBody());
}

public static String generateParam(String key, String value){
    String body = '--' + BOUNDARY;
    body += '\r\nContent-Disposition: form-data; name="' + key + '";\r\n\r\n' + value + '\r\n';
    return EncodingUtil.convertToHex(Blob.valueOf(body));
}

Basically, I am looking to bundle up Hulk and Iron Man along with Captain America and post to an endpoint:

form_data += generateParam('mail_to_name', 'The Hulk');
form_data += generateParam('mail_to_address', '123 Main St');
form_data += generateParam('mail_to_city', 'New York');
form_data += generateParam('mail_to_state', 'NY');
form_data += generateParam('mail_to_zip', '10166');
form_data += EncodingUtil.convertToHex(Blob.valueOf('--' + BOUNDARY + '--'));

and

form_data += generateParam('mail_to_name', 'Iron Man');
form_data += generateParam('mail_to_address', '123 Main St');
form_data += generateParam('mail_to_city', 'Austin');
form_data += generateParam('mail_to_state', 'TX');
form_data += generateParam('mail_to_zip', '10166');
form_data += EncodingUtil.convertToHex(Blob.valueOf('--' + BOUNDARY + '--'));

How do I accomplish that? I tried concatenating their respective hex strings and converted the string containing all 3 to a single blob and passed to my endpoint, but no dice.

TIA!

1 Answer 1

2

The --boundary-- ends the entire transmission. Make sure you only use it when you're done:

form_data += generateParam('mail_to_name', 'The Hulk');
form_data += generateParam('mail_to_address', '123 Main St');
form_data += generateParam('mail_to_city', 'New York');
form_data += generateParam('mail_to_state', 'NY');
form_data += generateParam('mail_to_zip', '10166');

form_data += generateParam('mail_to_name', 'Iron Man');
form_data += generateParam('mail_to_address', '123 Main St');
form_data += generateParam('mail_to_city', 'Austin');
form_data += generateParam('mail_to_state', 'TX');
form_data += generateParam('mail_to_zip', '10166');

form_data += EncodingUtil.convertToHex(Blob.valueOf('--' + BOUNDARY + '--'));

The exact formatting you'll need for the receiving end may vary, you'll have to consult their documentation.

1
  • I apologize for the disappointingly generic answer, but without knowing what the endpoint is and what data it expects, it's difficult to be more precise.
    – sfdcfox
    Jun 14, 2022 at 20:32

You must log in to answer this question.

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