You need to sign in to do that
Don't have an account?
![saleforce bee saleforce bee](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
HTTP file post with some parameters
Use case:- upload csv file to 3rd party site.
I am facing issues with http file post (.csv file) along with some parameters (email, Sid…).
It would be great if suggest some idea pseudo code or code snippet . Pls refer my code and correct me where I am not able to post file.
--------------------------------------------------------------
<!-- String postbody;
//Post body
//String boundary = '__boundary__1234454545445xxx';
String boundary = 'oMiJCBHGVvZmU7s3FcUGXMbyU23aX_Ow';
String queryheader = '--'+boundary+'\n';
String header = '--'+boundary+'\n'
+ 'Content-Disposition: form-data; name="file"; filename="'+filename
+'"\nContent-Type: application/octet-stream\n\n';
//+'"\nContent-Type: application/vnd.ms-excel\n\n';
String footer = '\n--'+boundary+'--';
//String footer = '\n--'+boundary;
String body = EncodingUtil.base64Encode(filebody); //encodes the blob into a base64 encoded String
//String body = filebody.toString();
//encodes the blob into a base64 encoded String
filecontent= header + filebody + footer ;
querybody= queryheader +'Content-Disposition: form-data; name="email"\n\n'+EncodingUtil.urlEncode('mail12345@gmail.com', 'UTF-8')+'\n';
querybody= querybody+queryheader +'Content-Disposition: form-data; name="sid"\n\n'+EncodingUtil.urlEncode('20', 'UTF-8')+'\n';
querybody= querybody+queryheader +'Content-Disposition: form-data; name="field"\n\n'+EncodingUtil.urlEncode('EMAIL:INITIALIZE', 'UTF-8')+'\n';
querybody= querybody+queryheader +'Content-Disposition: form-data; name="field"\n\n'+EncodingUtil.urlEncode('SID', 'UTF-8')+'\n';
postbody=querybody+filecontent;
/*********************************call API 2************************/
// content type:Content-Type: multipart/form-data; boundary=
HttpRequest req1 = new HttpRequest();
HttpResponse res1 = new HttpResponse();
Http http1 = new Http();
req1.setHeader('Content-type','multipart/form-data; boundary='+boundary);
//req1.setHeader('Content-type','multipart/mixed; boundary='+boundary);
req1.setHeader('Content-Disposition', 'attachment;filename='+filename);
//req1.setHeader('Content-type', 'application/vnd.ms-excel');
req1.setHeader('Cookie',setcookie) ;
req1.setMethod('POST');
req1.setEndpoint('https://mailpostingapi.com/cgi-bin/api/load1');
req1.setBody(postbody);
//req1.setBody('hi');
//req1.setCompressed(true);
//req1.setTimeout(60000);
req1.setHeader('Content-Length',String.valueof(postbody.length()));
try {
res1 = http1.send(req1);
if (res1.getBody().indexOf('err')>-1)
{
system.debug('***********error occured');
system.debug('********'+res1.getbody());
output_cookie2 =res1.getbody();
}
else
{
output_cookie2 =res1.getbody();
system.debug('*****su***'+res1.getbody());
}
System.debug('status'+res1.toString());
return null;
}
catch(System.CalloutException e)
{
System.debug('Callout error: '+ e);
System.debug(res1.toString());
return null;
} --->
Hello,
Please refer following link for code snippets:
http://enreeco.blogspot.it/2013/01/salesforce-apex-post-mutipartform-data.html
I have already tried with same. But no luck.
How we can test 'post body' by some http client tool. So that i can get post body string from SFDC and try to debug by http client tool. Postman - REST Client doesn’t have option to post the body of the request.
Any suggest how to debug this and fix .
Post body string
--_oMiJCBHGVvZmU7s3FcUGXMbyU23aX_Ow
Content-Disposition: form-data; name="email"
we23%40gmail.com
--__oMiJCBHGVvZmU7s3FcUGXMbyU23aX_Ow
Content-Disposition: form-data; name="sid"
20944245
--__oMiJCBHGVvZmU7s3FcUGXMbyU23aX_Ow
Content-Disposition: form-data; name="field"
EMAIL%3AINITIALIZE
--__oMiJCBHGVvZmU7s3FcUGXMbyU23aX_Ow
Content-Disposition: form-data; name="field"
LNAME%3AINITIALIZE
--__oMiJCBHGVvZmU7s3FcUGXMbyU23aX_Ow Content-Disposition: form-data; name="field"
SID
--__oMiJCBHGVvZmU7s3FcUGXMbyU23aX_Ow
Content-Disposition: form-data; name="file"; filename="email.csv"
Content-Type: application/octet-stream
dW1hc2hhbmthci5wQHRjcy5jb20sdW1hc2hhbmthciwyMDk0NDI0NTU4DQpzaGFua2FyLnNhbmthcmlAZ21haWwuY29tLHNoYW5rYXIsMjA5NDQyNDU1OA==
--__oMiJCBHGVvZmU7s3FcUGXMbyU23aX_Ow--