2017-07-18 59 views
0

我打算将IBM Watson Document Conversion服务 与Salesforce集成。在将PDF传递给Salesforce中的IBM watson时出现“415:Media not supported”错误

从那里我无法直接发送我的pdf文件到沃森,我得到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()); 
    } 
} 

回答

0

要发送的config作为查询参数,但它应该是在身体。

这里是curl命令,让你正在尝试做的事:

curl -X POST \ 
    -u "{username}":"{password}" \ 
    -F config="{\"conversion_target\":\"answer_units\"}" \ 
    -F "[email protected];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。


UPDATE:文档转换服务已被废弃,但来自该服务的特征得到加强,迁移到Discovery service

相关问题