function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aamir KhanAamir Khan 

Message body missing in multipart/form-data while attaching image file

Hi All,

I want to add attachment with image and send to the given number via API.
I am able to send Message with x-www-form-urlencoded but when i am trying to send image with multipart/form-data then i am getting forbidden status in response with 403 status code. In response body "Message body missing".

Code : 
public static void uploadFileattach(Blob filebody, String filename, String reqEndPoint){
    String smessageFrom = '+19999999999';
    String smessageTo = '+19999999999';
    String messageBody = 'Hi Test message !';
          
    system.debug('----- Values in uploadFileattach filebody='+filebody+'filename='+filename+'reqEndPoint='+reqEndPoint);   
    
    String boundary = '----SomethingsBoundary7MA4YWxkTrZu0gW';
      String finalbody = boundary+'\nContent-Disposition: form-data; name="PhoneNumber"\n\n'+smessageFrom+'\n'
      +boundary+'\nContent-Disposition: form-data; name="To"\n\n'+smessageTo+'\n'
      +boundary+'\nContent-Disposition: form-data; name="File"; filename="'+filename+'";'
      +'\nContent-Type: image/png\n\n'+boundary;        
    
      String bodyEncoded = EncodingUtil.base64Encode(filebody);
      
         String sendBody;
      sendbody = finalbody+bodyEncoded;  
      
      HttpRequest req = new HttpRequest();
      req.setHeader('AdminAccountId', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX');
      req.setHeader('AdminPassword', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX');
      req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
      req.setMethod('POST');
      req.setEndpoint(reqEndPoint);
      req.setBody(sendbody);
      req.setTimeout(120000);
      
      String bodyString = req.getBody();
      System.debug('## Output body:'+bodyString );   
 
      Http http = new Http();
      HTTPResponse res = http.send(req);
      system.debug('----response----'+res.getBody());
      
}

When i use setBodyAsBlob showimg same response.