+ Start a Discussion
kito kidkito kid 

http post for mulitpart form

I am trying to post the csv data through http post to end point url.

Below is my code.

 

          string test = 'test1,test1@gmail.com'; // which will be get from DB and make as CSV string


          String header = '--'+boundary+'\n';  //boundary is random string
          String footer = '\n'+boundary+'--';
          
          String bodyText = 'Content-Disposition: form-data; name="upload";'
                        + '\nfilename="test.csv"'
                        + '\nContent-Type: text/csv'
                        + '\n' + test;
           
          String body = header + bodyText + footer;
          req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
          req.setHeader('Content-Length',String.valueof(body.length()));
          req.setMethod('POST');
          req.setEndpoint(myendpoint);
          req.setBody(body);
          req.setTimeout(60000);

 

The problem is I couldn't able to post the data.

I make query from DB and make the data as CSV string.

Then put those CSV string in request body.

 

The requirement is I need to post the file.

Is it because the request can't be done successfully as I am making CSV string and put into request body?

 

Can anyone give me headstart?

I have been stuck on this problem quite long time now.

 

PS: I even tested whether my request header are right or not with posttestserver.com with REST client firefox plug in. Still can't post. Pls check out the test result. http://www.posttestserver.com/data/2013/07/01/01.18.49320686031

Best Answer chosen by Admin (Salesforce Developers) 
kito kidkito kid
finally. I solved it with the help of sfcfoc from stackover flow. the boundary is not formatted well. Thanks to sfcfoc http://salesforce.stackexchange.com/questions/13441/http-post-for-mulitpart-form?noredirect=1#comment19286_13441

All Answers

Avidev9Avidev9
Could see you sending request in the above code .

Have you actually processed the same ? By doing something like

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

Hi Avidev9,

 

Yes. I do post like you have mentioned.

The problem is I am just getting 500 [Unhanled Exceptions] from the end point that I am trying to call.

 

1) My Question is whether my request format is correct or not? Can you take a look?

2) As I want to post CSV data in request body, the way I append CSV as String to request body is correct or not?

3) Any alternative approach (references) for posting multipart form data in the http post request.

 

Thanks ahead.

kito kidkito kid
finally. I solved it with the help of sfcfoc from stackover flow. the boundary is not formatted well. Thanks to sfcfoc http://salesforce.stackexchange.com/questions/13441/http-post-for-mulitpart-form?noredirect=1#comment19286_13441
This was selected as the best answer