Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save douglascayers/784a38d3e08a21401103b0d6aff0cc92 to your computer and use it in GitHub Desktop.
Chatter REST API "/connect/files/users/me" resource to post single file.
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/files/users/me');
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="fileData"; filename="FileA.txt"' + '\r\n' +
'Content-Type: application/octet-stream; charset=ISO-8859-1' + '\r\n' + '\r\n' +
'aaa' +
footer
);
req.setMethod('POST');
req.setTimeout(60000);
HttpResponse res = new Http().send( req );
System.debug( res );
System.debug( res.getBody() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment