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
chandrasekhar reddychandrasekhar reddy 

HTTP multipart POST ?

   
curl --data "api_user=user&api_key=pwd&to=mymail@gmail.com&toname=Ram&subject=Testtt&from=othermail@gmail.com&text=testingtextbody&--form files[attachment.pdf]=@http://www.tutorialspoint.com/java/pdf/java_strings.pdf;type=application/pdf" https://someendpoint.com/api/mail.send.xml


I have above the cURL command. I want to convert above call to HTTP multipart POST because above request has a pdf attachment. I am doing as below. Is my multipart POST request construction correct?

   
String boundary = 'delimiter';
      String header = '--'+boundary+'\n';  //boundary is random string
      String footer = '\n\n--'+boundary+'--'; // blank line separates body/footer
      String test='jjjjj';
      String bodyText = 'Content-Disposition: form-data; name="files[myfile.pdf]";\n'
                    + 'filename="http://www.tutorialspoint.com/java/pdf/java_strings.pdf"\n'
                    + 'Content-Type: application/pdf\n\n' // Blank line separates header/body
                    + test;
    
    
    bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="api_user"\r\n\r\n superuser123';
    bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="api_key"\r\n\r\n superpwd12345';
    bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="to"\r\n\r\n myemail@gmail.com';
    bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="from"\r\n\r\n someother@gmail.com';
    bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="text"\r\n\r\n hellooooooooooooooooooooooooo';
    
    
      String body = header + bodyText + footer;
      HttpRequest req = new HttpRequest();
    
      req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
      req.setHeader('Content-Length',String.valueof(body.length()));
      req.setMethod('POST');
      req.setEndpoint('https://someendpoint.com/api/mail.send.xml');
      req.setBody(body);
      req.setTimeout(60000);
      Http http = new Http();
      HTTPResponse res = http.send(req);
      System.debug('response:'+req.getBody());


Thanks!