0
votes

I need to integrate salesforce & expensify by using this API and test the API here. Since I need to make the request from salesforce , I could only use apex to implement it. All I need is to send a acceptable multipart/form-data Post request ( include a JSON and a CSV file) to expensify.

I have searched a lot like http://blog.enree.co/2013/01/salesforce-apex-post-mutipartform-data.html and https://developer.salesforce.com/forums/?id=906F000000090JcIAI and My code is like this :

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'+'[email protected],[email protected],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);

The problem is After sending a request, I get a response like

{"responseMessage":"Error in multipart initialization","responseCode":500}

Maybe it is caused by encoding type or something I don't know.

I was really confused, can anybody help? If you need some further information, please tell me!

1

1 Answers

0
votes

Finally I solved it by myself inspired by some javascript demo code. The promblem of my above code is that it isn't fully up to the Http standard and it was caused by the "\n\r" things in the http request. The code works is like this

    //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'+'[email protected],[email protected],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);