Reputation: 49
I am using IDOL API to convert a pdf file to html in Salesforce Apex class.
When I am using simple html, like:
<html>
<body>
<form action="https://api.idolondemand.com/1/api/sync/viewdocument/v1"
method="Post" enctype="multipart/form-data">
<input type="hidden" name="raw_html" value="true"/>
<input type="hidden" name="apikey"
value="{apikey removed}"/>
<input type='FILE' name='file'/><br/>
<input type='submit' name='upload_btn' value='Submit'/>
</form>
</body>
</html>
then this is working. But when I am using APEX code httprequest
, then this is making a bad request.
My APEX code is:
public HTTPResponse uploadFile(Attachment file){
//String boundary = '---------------------\n';
String header = '\n';
header += 'Content-Disposition: form-data; name="file"; filename="'
+ file.Name +'"\nContent-Type: application/pdf\n\n';
String footer = '\n';
String querybody = '\n Content-Disposition: form-data;
name="raw_html"\n\n'+true+'\n';
querybody = querybody + '\n Content-Disposition: form-data;
name="apikey"\n\n {apikey removed} \n\n';
//base64 encoded body
String bodyEncoded = EncodingUtil.base64Encode(file.body);
//last encoded body bytes
String last4Bytes =
bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
//if the last 4 bytes encoded base64 ends with the padding character (=
or ==) then re-encode those bytes with the footer
//to ensure the padding is added only at the end of the body
if(last4Bytes.endsWith('='))
{
Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
HttpRequest tmp = new HttpRequest();
tmp.setBodyAsBlob(decoded4Bytes);
String last4BytesFooter = tmp.getBody()+footer;
bodyEncoded = querybody + header +
bodyEncoded.substring(0,bodyEncoded.length()-4) +
EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)) +
footer;
}
else
{
bodyEncoded = querybody + header + bodyEncoded + footer;
}
bodyEncoded = querybody;// + header + bodyEncoded + footer;
System.debug('>>>>>>>>>>>>>>>>>>>>>>' + bodyEncoded);
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','multipart/mixed');
req.setMethod('POST');
req.setEndpoint('https://api.idolondemand.com/1/api/sync/viewdocument/v1'
);
req.setBody(bodyEncoded);
req.setTimeout(120000);
req.setHeader('Content-Length', String.valueof(req.getBody().length()));
Http http = new Http();
Httpresponse res = http.send(req);
return res;
}
My Visual force page containing only:
<apex:page controller="IDOLService">
<apex:form >
<apex:pageBlock title="File Input">
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:inputFile value="{!file.body}" filename="{!file.Name}"/>
<apex:commandButton action="{!showPreview}" value="Show Preview"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:outputpanel id="docpanel">
{!result}
</apex:outputpanel>
</apex:form>
</apex:page>
This is giving me error of 7005, bad request.
Upvotes: 1
Views: 422
Reputation: 349
We tried reproducing using the supplied code but it’s hard to reproduce properly without the remainder of the IDOLService controller code that has seemingly been omitted. We haven’t tried http posting multi-part forms from apex before (and a glance at some other questions suggests it’s not entirely simple) so it will take a bit of time to put together a working example.
Please can you send more information to idolondemand(at)hp.com so that the team can do a more in-depth review?
Regards,
hughesthe1st
(I work for HP)
Upvotes: 1