如何将文件从 Salesforce Apex 上传到 IBM Watson

How to upload a file from Salesforce Apex to IBM Watson

我正在尝试将示例文档从 Salesforce Apex class 上传到 IBM Watson。我正在尝试使用以下代码片段,但出现此错误

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

不知何故超时了。

        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);

如果您持续看到此错误,请在 SDK 的 GitHub 存储库中提出问题。

https://github.com/watson-developer-cloud/salesforce-sdk/issues

IBM Watson Salesforce SDK 出现问题,这就是您超时的原因。此问题已在 pull request and released in v1.4.0.

中修复

问题是我们发送了错误的 mime-type (APPLICATION_OCTET_STREAM),并且我们没有在多部分请求中发送文件名。