1

我在 Salesforce Apex 类中使用 IDOL API 将 pdf 文件转换为 html。

当我使用简单的 html 时,例如:

<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>

那么这是有效的。但是当我使用 APEX 代码时httprequest,这是一个错误的请求。

我的 APEX 代码是:

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;
    }

我的视觉力量页面仅包含:

<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>

这给了我 7005 错误,错误的请求。

4

1 回答 1

1

我们尝试使用提供的代码进行重现,但如果没有似乎已省略的 IDOLService 控制器代码的其余部分,很难正确重现。我们之前没有尝试过从 apex 发布多部分表单的 http (并且查看其他一些问题表明它并不完全简单),因此需要一些时间来整理一个工作示例。

请您将更多信息发送到idolondemand(at)hp.com,以便团队进行更深入的审查?

问候,

Hughesthe1st
(我为惠普工作)

于 2015-03-02T19:04:28.183 回答