0

I am trying to make a POST request to Eventbrite API to send an image from Salesforce along with other parameters. Under the hood Eventbrite is using Amazon AWS S3 bucket. When I send the request using postman, I am able to upload the file and it works without issues. See screenshot below.

enter image description here

When I tried the same using Apex, I converted the versiondata from ContentVersion object to a base64Encode string and sent the request. But EventBrite is returning a error response. See image below.

enter image description here

This is my apex code. I am not sure how to go about fixing this issue. I read numerous articles online about the same issue but couldn't understand how to pass a file from Salesforce to external system as a binary. It's also unclear from the Eventbrite documentation on the file format it is accepting.

Apex Class:

public class FileUploadClass{
    
    private final static string BOUNDARY = '----sfdc-multi-form';
    
    public static void submit(String access_token){
        
        ContentVersion cv=[select id,ContentDocumentId,versiondata from Contentversion LIMIT 1];
        Blob image =cv.VersionData;
        String imageAsString = EncodingUtil.base64Encode(image);
        system.debug(imageAsString);
        
        String form_data = '';
        form_data += generateParam('AWSAccessKeyId', 'xx');
        form_data += generateParam('bucket', 'eventbrite-uploader-incoming-prod');
        form_data += generateParam('acl', 'private');
        form_data += generateParam('key', 'xxx');
        form_data += generateParam('signature', 'xxx=');
        form_data += generateParam('policy', 'xxx');
        form_data += generateParam('file', imageAsString);
        
        form_data += EncodingUtil.convertToHex(Blob.valueOf('--' + BOUNDARY + '--'));
        
        Blob form_data_blob = EncodingUtil.convertFromHex(form_data);
        
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://s3.amazonaws.com/eventbrite-uploader-incoming-prod/');
        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());
        system.debug(response.getStatus());
    }
    
    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));
    }
}
2

0

You must log in to answer this question.

Browse other questions tagged .