Salesforce HttpPost的编码类型?(salesforce & expensify整合)

Encoding type of Salesforce HttpPost?(Integration of salesforce & expensify)

我需要使用此 API and test the API here 来集成 salesforce 和 expensify。 由于我需要从 salesforce 发出请求,所以我只能使用 apex 来实现它。 我只需要发送一个可接受的 multipart/form-data Post 请求(包括一个 JSON 和一个 CSV 文件)以进行 expensify.

我搜索了很多类似 http://blog.enree.co/2013/01/salesforce-apex-post-mutipartform-data.html and https://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}

可能是编码类型或其他我不知道的原因。

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

最后我在一些javascript演示代码的启发下自己解决了这个问题。我上面代码的问题是它不完全符合Http标准,它是由http请求中的“\n\r”引起的。代码是这样的

    //Create a Json and convert it to String
    JSONGenerator gen=JSON.createGenerator(true);
    gen.writeStartObject();
        gen.writeStringField('type', 'update');
        gen.writeFieldName('credentials');
            gen.writeStartObject();
            gen.writeStringField('partnerUserID', 'userID');//Should be changed 
            gen.writeStringField('partnerUserSecret', 'token');//Should be changed
            gen.writeEndObject();
        gen.writeFieldName('inputSettings');
            gen.writeStartObject();
            gen.writeStringField('type', 'employees');
            gen.writeStringField('policyID', 'ID');//the policyID corresponding to the policy which the employees will be added to;
            gen.writeStringField('fileType', 'csv');
            gen.writeEndObject();
    gen.writeEndObject();
    String requestJobDescription=gen.getAsString();

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

  String footer = '--'+boundary+'--';             
  String body=header1+header2+CSVString+'\r\n'+footer;//the CSVString is like 'EmployeeEmail,ManagerEmail,Admin\n'+'asidoj@qwe.com,qwdmka@ee.com,FALSE'

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