首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将pdf传递给Salesforce中的IBM watson时出现"415:Media not supported“错误

将pdf传递给Salesforce中的IBM watson时出现"415:Media not supported“错误
EN

Stack Overflow用户
提问于 2017-07-18 17:26:40
回答 1查看 81关注 0票数 0

我计划将IBM Watson文档转换服务与Salesforce集成。

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

我也得到了这个错误:

代码语言:javascript
复制
{
  "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"
}  

下面是我使用的代码:

代码语言:javascript
复制
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());
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-02-18 09:40:54

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

下面是curl命令,它实现了您正在尝试执行的操作:

代码语言:javascript
复制
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类复制代码。它将帮助您将Attachment编码为base64,以便您可以将其结束为Watson。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45162680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档