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!