3

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

on salesforce

  • 1
    The posttestserver.com text includes at least a partial answer to your problem. You didn't specify a file size, and so the server rejected the request. Note that the error code was 3. Try including a Content-Length header. – sfdcfox Jul 1 '13 at 12:15
  • hi sdcfox, I idd include Content-Length in request header when I do both with coding and test with REST client. req.setHeader('Content-Length',String.valueof(body.length())); – kitokid Jul 2 '13 at 2:07
  • Two problems here, actually. (1) footer should be '\n--'+boundary+'--' (you forgot the leading --), and (2) I'm fairly sure you need TWO \n between the mime headers and the file body, plus two between the body and the footer (e.g. a blank line should appear between each). Meaning that footer should probably be '\n\n--'+boundary+'--', and header should have +'\n\n' instead of +'\n'; – sfdcfox Jul 2 '13 at 3:28
  • My first comment was simply me mis-reading the error. You can see the error message about a file being declared but not uploaded, which suggests that the content wasn't properly delimited. – sfdcfox Jul 2 '13 at 3:31
  • hi sfcfoc.. you saved my day. I have been stuck on this problem for so long.. :'( . could you pls answer this with your last comment. Then I will accept it as answer. Thanks you so much. Now it is working .. yahooooooooooooooo. – kitokid Jul 2 '13 at 3:54
4
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\n--'+boundary+'--'; // blank line separates body/footer

  String bodyText = 'Content-Disposition: form-data; name="upload";\n'
                + 'filename="test.csv"\n'
                + 'Content-Type: text/csv\n\n' // Blank line separates header/body
                + 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);
  • fwiw I had to replace \n with \r\n for my nodejs rest endpoint to parse correctly using busboy. – gcek2 Mar 5 '15 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.