2017-07-18 52 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

相關問題