0

I a creating an rest api at apex class to post a file upload but each time i am getting an Bad request, i have tried everything but still no breakthrough. Here is my code as using the blob method to send the body.

String separationString = 'gfdsgtdfhgfdghjdgjg';

String boundary = '----------------------------741e90d31eff';
      String header = '--'+boundary+'\r\nContent-Disposition: form-data; name="Lambdas"; filename="D:\\Document 3.pdf"\r\nContent-Type: '+fileType+''; 
      String footer = '\r\n--'+boundary+'--';              
      String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
      while(headerEncoded.endsWith('='))
      {
       header+=' ';
       headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
      }
      String bodyEncoded = EncodingUtil.base64Encode(Blob.valueOf(fileContents));
      String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));

      Blob bodyBlob = null;
      String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
      if(last4Bytes.endsWith('='))
      {
           Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
           HttpRequest tmp = new HttpRequest();
           tmp.setBodyAsBlob(decoded4Bytes);
           String last4BytesFooter = tmp.getBody()+footer;   
           bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.substring(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
      }
      else
      {
            bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
      }

      HttpRequest req = new HttpRequest();
      req.setHeader('Content-Type','multipart/form-data; boundary='+separationString+'');
      req.setMethod('POST');
      req.setEndpoint(targetURL);
      req.setBodyAsBlob(bodyBlob);
      req.setTimeout(120000);         

  req.setTimeout(120000); 

   System.debug('req' + bodyBlob.toString()+''+bodyBlob.size());
   String response = '';
  try {           
     Http http = new Http();   
     HttpResponse httpResponse = http.send(req); 

And here is a screen shot of POSTMAN console to make a request.

Request Headers:
Accept:"application/json"
Content-Type:"multipart/form-data"
cache-control:"no-cache"
Postman-Token:"a7153dfb-c0f8-4354-9aea-c1139618c2f4"
Authorization:"Bearer xxxxxxx"
Request Body:
name:"Lambdas"
file:
_events:
_eventsCount:3
_readableState:
autoClose:true
bytesRead:45855
closed:true
domain:null
fd:null
flags:"r"
mode:438
path:"C:\Users\mukul_agrawal\Pictures\AAkADLk.jpg"
readable:false

can anybody help me out to correct the code and the request body part. Thanks in Advance.

  • Make sure you log out/expire that session. I've removed it from your question's body, but it's still available in the edit history. – sfdcfox Apr 8 at 14:50
  • @sfdcfox thanks for the correction. – Mukul Agrawal Apr 8 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.