Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Last active March 17, 2017 04:11
Show Gist options
  • Select an option

  • Save douglascayers/f9d1f881cd46b252784d8920b6912691 to your computer and use it in GitHub Desktop.

Select an option

Save douglascayers/f9d1f881cd46b252784d8920b6912691 to your computer and use it in GitHub Desktop.
Chatter REST API "/connect/batch" resource to post multiple files
String header = '--some_random_boundary_string' + '\r\n';
String separator = '\r\n' + '--some_random_boundary_string' + '\r\n';
String footer = '\r\n' + '\r\n' + '--some_random_boundary_string--';
HttpRequest req = new HttpRequest();
req.setEndpoint( URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v39.0/connect/batch');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'multipart/form-data; boundary=some_random_boundary_string');
req.setHeader('Accept', 'application/json');
req.setBody(header +
'Content-Disposition: form-data; name="json"' + '\r\n' +
'Content-Type: application/json' + '\r\n' + '\r\n' +
'{ "haltOnError" : true, "batchRequests" : [' +
'{ "url": "/v39.0/connect/files/users/me", "method":"POST", "binaryPartName":"binaryPart1", "binaryPartNameAlias":"fileData" },' +
'{ "url": "/v39.0/connect/files/users/me", "method":"POST", "binaryPartName":"binaryPart2", "binaryPartNameAlias":"fileData" }' +
']}' + '\r\n' + '\r\n' +
separator +
'Content-Disposition: form-data; name="binaryPart1"; filename="FileA.txt"' + '\r\n' +
'Content-Type: application/octet-stream; charset=ISO-8859-1' + '\r\n' +
'Content-Transfer-Encoding: binary' + '\r\n' + '\r\n' +
'aaa' + // content for FileA
separator +
'Content-Disposition: form-data; name="binaryPart2"; filename="FileB.txt"' + '\r\n' +
'Content-Type: application/octet-stream; charset=ISO-8859-1' + '\r\n' +
'Content-Transfer-Encoding: binary' + '\r\n' + '\r\n' +
'bbb' + // content for FileB
footer
);
req.setMethod('POST');
req.setTimeout(60000);
HttpResponse res = new Http().send( req );
System.debug( res );
System.debug( res.getBody() );
@douglascayers

Copy link
Copy Markdown
Author

Seems to be a bug with this API where the binaryPartNameAlias is not being processed
http://salesforce.stackexchange.com/questions/164606/chatter-rest-batch-post-multiple-files-missing-expected-filedata-binary-param

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment