Unable to convert pdf to JSON in Salesforce using Watson document conversion

I am planning to integrate Watson service (Document Conversion) with salesforce server.
From there I am unable to send my file directly to your server and everytime I am getting the “Media Type not supported” . I am also getting this error: |{ “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” }
My code is:

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+’rnrn’));
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+’rnrn’));
}
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 = ‘rn’ + 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);
request.setHeader(‘conversion_target’,’answer_units’);
string username= ’10cdb8c2-fda8-4de8-84db-16ac25dc1ceb’;
string password= ‘6OXqSeEBVmWP’;
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());
}
}

Related:

Leave a Reply