salesforcewatson

How to upload a file from Salesforce Apex to IBM Watson


I am trying to upload a sample document from Salesforce Apex class to IBM Watson. I am trying with the following snippet but am getting this error

"23:13:17:000 USER_DEBUG "error" : "Request timed out, please try again."

Somehow it is getting timed out.

        Blob fileBlob = Blob.valueOf('This is a sample pdf file');
        IBMDiscoveryV1 discovery = new IBMDiscoveryV1('2017-11-07');
        IBMDiscoveryV1Models.AddDocumentOptionsBuilder builder = new IBMDiscoveryV1Models.AddDocumentOptionsBuilder(
                '<<EnvironmentId>>',
                '<<CollectionId>>');

        IBMWatsonFile.FileBuilder fileBuilder = new IBMWatsonFile.FileBuilder();
        fileBuilder.name('Sample.pdf');
        fileBuilder.body(fileBlob);
        IBMWatsonFile file = fileBuilder.build();
        builder.file(file);

        IBMDiscoveryV1Models.AddDocumentOptions options = builder.build();

        IBMDiscoveryV1Models.DocumentAccepted docAccepted = discovery.addDocument(options);

Solution

  • There was a problem with the IBM Watson Salesforce SDK and that's why you were getting a timeout. The issue was fixed in this pull request and released in v1.4.0.

    The problem was that we were sending the wrong mime-type (APPLICATION_OCTET_STREAM) and we were not sending the filename in the multi-part request.