Salesforce HttpPost的编码类型?(salesforce& amp; expensify)

时间:2015-07-29 06:43:35

标签: salesforce httprequest apex-code apex

我需要整合salesforce&使用此API进行费用排查并测试API here。 由于我需要从salesforce发出请求,因此我只能使用apex来实现它。 我需要的是发送一个可接受的multipart / form-data Post请求(包括一个JSON和一个CSV文件)来进行扩展。

我搜索了很多http://blog.enree.co/2013/01/salesforce-apex-post-mutipartform-data.htmlhttps://developer.salesforce.com/forums/?id=906F000000090JcIAI,我的代码是这样的:

JSONGenerator gen=JSON.createGenerator(true);
        gen.writeStartObject();
            gen.writeStringField('type', 'update');
            gen.writeFieldName('credentials');
                gen.writeStartObject();
                gen.writeStringField('partnerUserID', 'USERID');
                gen.writeStringField('partnerUserSecret', 'TOKEN');
                gen.writeEndObject();
            gen.writeFieldName('inputSettings');
                gen.writeStartObject();
                gen.writeStringField('type', 'employees');
                gen.writeStringField('policyID', 'policyID');
                gen.writeStringField('fileType', 'csv');
                gen.writeEndObject();
        gen.writeEndObject();                
        String requestJobDescription=gen.getAsString();
        Blob csv=Blob.valueOf('EmployeeEmail,ManagerEmail,Admin\n'+'asidoj@qwe.com,qwdmka@ee.com,FALSE');

      String boundary = '----------------------------wqo12loz741e90d31eff';
      String header2 = '--'+boundary+'\n'+ 'Content-Disposition: form-data; name="data"; filename="1.csv"'+'\nContent-Type: application/octet-stream\n\n';
      String header1andJSON = '--'+boundary+'\nContent-Disposition: form-data;  name="requestJobDescription" \n\r\n\r'+requestJobDescription+'\n';

      String footer = '--'+boundary+'--';             
      String body=header1andJSON+header2+csv.toString()+footer;
      HttpRequest req = new HttpRequest();
      req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
      req.setMethod('POST');
      req.setEndpoint('https://integrations.expensify.com/Integration-Server/ExpensifyIntegrations');
      req.setBody(body);
      req.setTimeout(120000);

      Http http = new Http();
      HTTPResponse res = http.send(req);

问题是在发送请求后,我收到了像

这样的回复
{"responseMessage":"Error in multipart initialization","responseCode":500}

可能是由编码类型或我不知道的东西引起的。

我真的很困惑,有人可以帮忙吗? 如果您需要更多信息,请告诉我!

1 个答案:

答案 0 :(得分:0)

最后,我自己解决了一些javascript演示代码的启发。我上面代码的问题在于它不完全符合Http标准,它是由" \ n \ r" http请求中的内容。代码就像这样

<!-- :: Batch section
@echo off
setlocal

cls
echo Install Windows 8.1?
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo Reply: "%HTAreply%"
goto :EOF
-->


<HTML>
<HEAD>
<HTA:APPLICATION BORDER="none" SCROLL="no" >

<SCRIPT language="JavaScript">
window.resizeTo(1100,200);

function closeHTA(reply){
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   fso.GetStandardStream(1).WriteLine(reply);
   window.close();
}

</SCRIPT>
<style type="text/css">
body { 
   margin-left: 10%; 
   margin-right: 10%; 
   color: white;
   background: #000099;
   font-family: "Lucida Grande", Verdana, Helvetica, Arial, sans-serif;
}
</style>
</HEAD>
<BODY>
   <h1>Get Windows 8.1 for free</h1>
   <p>Go to the Store to see what's new and get the update. You can keep using your PC while it downloads.<br><br>
   <div align="right">
   <button onclick="closeHTA('Yes');">Go to the Store</button>&nbsp;
   <button onclick="closeHTA('No');">No thanks</button>
   </div>
</BODY>
</HTML>