0

我计划将 IBM Watson Document Conversion 服务与 Salesforce 集成。

从那里我无法将我的 pdf 文件直接发送给 Watson,我得到了Media Type not supported.

我也收到此错误:

{
  "code" : 500 ,
  "error" : "Server Error" ,
  "description" : "2017-07-18T06:02:19-04:00, Error WATSNGWERR-0x0113001c occurred when accessing https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document?version=2015-12-15&config="{"conversion_target":"answer_units"}", Tran-Id: gateway-dp02-1967135880 - Watson Gateway Error"
}  

这是我正在使用的代码:

public class Resume {
  String boundary = '----------------------------741e90d31eff';

  public string id{get;set;}
  public string content{get;set;}
  Transient public Attachment att{set;get;}

  public Resume(ApexPages.StandardController controller) {
    id=ApexPages.currentPage().getParameters().get('id');
    att=new Attachment();
    att=[Select Id,ParentId, Name,body,ContentType From Attachment where ParentId=:id limit 1];
    content=String.valueOf(att.body);

    System.debug('---->' + content);
    String header = '--' + boundary + '\nContent-Disposition: form-data; name="att"; filename="'+att.name+'";\nContent-Type: application/pdf';
    String footer = '--' + boundary + '--';
    String headerEncoded =
    EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
    String bodyEncoded = EncodingUtil.base64Encode(att.body);
    Blob bodyBlob = null;
    String last4Bytes =
    bodyEncoded.substring(bodyEncoded.length() - 4, bodyEncoded.length());
    while (headerEncoded.endsWith('=')){
      header+=' ';
      headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
    }

    if (last4Bytes.endsWith('==')) {
      last4Bytes = last4Bytes.substring(0,2) + '0K';
      bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
      String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
      bodyBlob = EncodingUtil.base64Decode(headerEncoded + bodyEncoded + footerEncoded);
    } else if (last4Bytes.endsWith('=')) {
      last4Bytes = last4Bytes.substring(0,3) + 'N';
      bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
      footer = '\n' + footer;
      String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
      bodyBlob = EncodingUtil.base64Decode(headerEncoded + bodyEncoded + footerEncoded);
    } else {
      footer = '\r\n' + footer;
      String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
      bodyBlob = EncodingUtil.base64Decode(headerEncoded + bodyEncoded + footerEncoded);
    }

    String configAsString ='\"conversion_target:answer_units\"';
    Http h = new Http();
    HttpRequest request = new HttpRequest();
    request.setMethod('POST');
    request.setHeader('Content-Type','multipart/form-data; boundary=' + boundary);

    String username= 'DOCUMENT-CONVERSION-USERNAME';
    String password= 'DOCUMENT-CONVERSION-PASSWORD';
    request.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(blob.valueOf(username + ':' + password)));

    request.setEndpoint('https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document?version=2015-12-15&config='+configAsString);
    request.setBodyAsBlob(bodyBlob);
    request.setCompressed(true);
    HttpResponse response = h.send(request);
    System.debug(response.getBody());
  }
}
4

1 回答 1

0

您将config作为查询参数发送,但它应该在正文中。

这是使您尝试执行的操作的 curl 命令:

curl -X POST \
  -u "{username}":"{password}" \
  -F config="{\"conversion_target\":\"answer_units\"}" \
  -F "file=@sample.pdf;type=application/pdf" \
"https://gateway.watsonplatform.net/document-conversion/api/v1/convert_document?version=2015-12-15"

我还认为您创建身体的方式存在错误。我的团队构建了一个 SDK 以在 Salesforce 环境中使用 Watson API。我建议你看看。

如果您无法将 SDK 部署到您的 Salesforce 组织(代码很多),请从IBMWatsonMultipartBody.cls类中复制代码。它将帮助您将 a 编码Attachment为 base64,以便您可以将其结束于 Watson。


更新:文档转换服务已被弃用,但该服务的功能已得到增强并迁移到发现服务

于 2018-02-18T01:40:53.530 回答