+ Start a Discussion
Sameer Rath 4Sameer Rath 4 

HTTP request for Upload a File

Hi all,
I am learning box integration and facing some problem in uploading files to box. What should be the HTTP request to be set for uploading a file in box ?
Can any body please help me out. 

Thank You.
Gaurav KheterpalGaurav Kheterpal
Try this example code, it should work uploading to your Box account.
 
public static HTTPResponse uploadFile(Attachmnet file)

 {
  String boundary = '__boundary__xxx';
  String header = '--'+boundary+'\n';
     + 'Content-Disposition: form-data; name="data"; filename="'+file.name
     +'"\nContent-Type: application/octet-stream\n\n';
  String footer = '\n--'+boundary+'--';
  String body = EncodingUtil.base64Encode(file.Body); //encodes the blob into a base64 encoded String body = header + body + footer;
  HttpRequest req = new HttpRequest();
  req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
  req.setMethod('POST');
  req.setEndpoint('http://posttestserver.com/post.php?dir=what_a_wonderful_post');   
  req.setBody(body);

  req.setTimeout(60000);
  req.setHeader('Content-Length',String.valueof(body.length()));
  Http http = new Http();
      return http.send(req);
 }

 
sfdc550sfdc550
Check the link i am sure it will help u
https://github.com/dhoechst/ApexBox

 
Sameer Rath 4Sameer Rath 4
Hi Gaurav,
Still problem has not solved. Its showing 405 error. [System.HttpResponse[Status=Method Not Allowed, StatusCode=405] ]