1

I am currently uploading files to a third-party storage space through its API. This is the API through Java which was uploaded:

HttpPost post = new HttpPost(url);
            post.setHeader("Token", token);
            post.setHeader("UploadPath",
                    Base64.encodeBase64String(uploadPath.getBytes());
            post.setHeader("FileName", Base64.encodeBase64String(fileName.getBytes()));
            post.setHeader("FileSize", Long.toString(fileSize));
            post.setHeader("FileModifiedtime", Long.toString(lastModified));
            post.setHeader("FileEOF", "0");
            post.setEntity(new ByteArrayEntity(IOUtils.toByteArray(new FileInputStream(fileEntry))));

I tried doing this in Apex as:

    HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setMethod('POST');
req.setHeader('Token', token);
req.setHeader('UploadPath', EncodingUtil.base64Encode(Blob.valueOf(uploadPath)));
req.setHeader('FileName', EncodingUtil.base64Encode(Blob.valueOf(fileName)));
req.setHeader('FileSize', String.valueOf(fileSize));
req.setHeader('FileModifiedtime', String.valueOf(lastModified.getTime()/1000));
req.setHeader('FileEOF', '0');
req.setEndpoint(url);
req.setBodyAsBlob(bodyBlob);
req.setTimeout(60000);

The server returns 400 if the request body is empty or the header fields are missing. I have also tried req.setBody(EncodingUtil.base64Encode(bodyBlob)). Both the options in Apex yield 500 from the server side. Have checked this and it seems that the requirement is different, sending multi-part form data. However, the server does not accept this type and checks through the body content and the header values. How do I proceed about this?

P.S.: This kind of upload feature is for Document/Attachment body fields.

SOLUTION: Use the header and chunk it to a byte format as mentioned in http://blog.enree.co/2013/01/salesforce-apex-post-mutipartform-data.html#finalCode_multipart Have to change the server code with base64encoded string. Worked like a charm!

  • Have you made your remote webservice web site in your remote site settings list? – Lance Shi Jun 24 '15 at 3:21
  • Yes, I'm able to connect to the remote site. The only thing that I'm not able to perform is to upload a file from Salesforce to my remote site. – dmachop Jun 24 '15 at 14:12
1
post.setEntity(new ByteArrayEntity(IOUtils.toByteArray(new FileInputStream(fileEntry)))); // Binary

is different from

req.setBodyAsBlob(bodyBlob); // Blob

both are different data types. So, it is expected that code won't work.

Even if you send the blob converting to Base64 it would not work becasue expected body type is different.

One of the solutions can be is change third party server to read base64 into body and then convert to binary in background. Salesforce support Blob and Base64 only. Binary type is not supported.

|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.