title: multi-part post from Apex in salesforce to salesforce API endpoint for an image
body: <p>I want to do a multi-part post from Apex to salesforce itself for an image. And salesforce expects binary data for the image. However we need to prefix the header with boundary as part of multipart convention and append footer so that receiving end (salesforce in this case) can parse the same and process the data(Image in this case). The issue is image is a blob and headers and footers are string. And we cannot directly concatenate these 3 parts in Apex to send to SF. </p>
  
  <p>One possible way suggested by people who had similar issues was use base64encoded to convert blob to string and concatenate the header and footer. This is good and works but only 33% of times. Reason is base64 encdoing appends "=" or "==" sign to padd at the end if the source blob data bytes are not exact multiple of 3 (This is how base64encoding works). So these padded characters creates issues if they are appended while encoding. There is no fruitful way i am able to figure out to resolve this final issue and I get 400 bad request by SF. There was a way suggested by @Nerd in his blog but that resolves the error from 400 to a successful 201 return value. But the background issue is that the way the "=" characters were removed actually corrupted the blob data, so receiving end receives a valid request but NOT a valid content and thus image is not really uploaded and assigned to the profile.</p>
  
  <p>To avoid the above concatenation problem, I also tried to set Content-Transfer-Encoding as "base64" and sending only the image as base64encoded and header/ footers as normal string, however that request is also generating 500 server error.</p>
  
  <p>Has anyone faced any similar issues or has any pointers for any workaround either for concatenating the header, footer string to blob body or else for resolving the padded "=" characters and not corrupting the data. Or has anybody successfully tried using base64 as Content-Transfer-Encoding method to send multi-part post? Any help is highly appreciated.</p>
  
  <p>Kamal</p>