+ Start a Discussion
Jordan@BracketLabsJordan@BracketLabs 

Post Attachments out of APEX

I'm trying to send a file that's attached to a Case out of my APEX Code, I'm trying to 'form' the POST request and pass the attachments body in the post request as follows:

 

HttpRequest req = new HttpRequest();
req.setHeader('Authorization','Basic '+EncodingUtil.base64Encode(Blob.valueOf('----:0000')));
req.setHeader('Content-Type','multipart/form-data; boundary=-----------------------------153501500631101');
req.setHeader('Content-Length',String.valueOf(attachments[0].BodyLength));
req.setMethod('POST');  
req.setEndpoint(endPoint+'/createattachment');
		
String body = '-----------------------------153501500631101\r\n';
body = body + 'Content-Disposition: form-data; name="Filedata"; filename="'+attachments[0].Name+'"\r\n';
body = body + 'Content-Type: '+attachments[0].ContentType+'\r\n\r\n';
body = body + attachments[0].Body + '\r\n';
body = body + '-----------------------------153501500631101--\r\n';
    req.setbody(body);
try{
  Http http = new Http();
  HTTPResponse res = Http.send(req);
}

 Everything looks good in the debugger (which reports the body as being 'Blob', even though it's a string ?) 

 

And the server sends back a 200, but the attachment doesn't appear?

 

I've added this question to stackoverflow.com: http://stackoverflow.com/questions/10215808/post-multipart-form-data-out-of-salesforce-com-apex

mesmailmesmail

Hey Jordan,

Did you figure this out?

almazanjlalmazanjl

Hi.

 

Try this solution. Valid for servers that only support binary content and for servers that support base64 encoding.

 

http://salesforceafondo.wordpress.com/2013/01/08/post-multipartform-data-out-of-salesforce-com-with-apex/

 

Hope this helps.

 

José Luis Almazán.