curl --data "api_user=user&api_key=pwd&to=mymail@gmail.com&toname=Ram&subject=Testtt&from=othermail@gmail.com&text=testingtextbody&--form files[attachment.pdf]=@http://www.tutorialspoint.com/java/pdf/java_strings.pdf;type=application/pdf" https://someendpoint.com/api/mail.send.xml
我在cURL命令之上。我想将上面的调用转换为HTTP多部分POST,因为上面的请求有一个pdf附件。我正在做如下。我的多部分POST请求构造是否正确?
String boundary = 'delimiter';
String header = '--'+boundary+'\n'; //boundary is random string
String footer = '\n\n--'+boundary+'--'; // blank line separates body/footer
String test='jjjjj';
String bodyText = 'Content-Disposition: form-data; name="files[myfile.pdf]";\n'
+ 'filename="http://www.tutorialspoint.com/java/pdf/java_strings.pdf"\n'
+ 'Content-Type: application/pdf\n\n' // Blank line separates header/body
+ test;
bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="api_user"\r\n\r\n superuser123';
bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="api_key"\r\n\r\n superpwd12345';
bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="to"\r\n\r\n myemail@gmail.com';
bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="from"\r\n\r\n someother@gmail.com';
bodyText += '--'+boundary+'\r\nContent-Disposition: form-data; name ="text"\r\n\r\n hellooooooooooooooooooooooooo';
String body = header + bodyText + footer;
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
req.setHeader('Content-Length',String.valueof(body.length()));
req.setMethod('POST');
req.setEndpoint('https://someendpoint.com/api/mail.send.xml');
req.setBody(body);
req.setTimeout(60000);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('response:'+req.getBody());
谢谢!
答案 0 :(得分:0)
阅读以下有关此主题的文章
POST Mutipart/form-data with HttpRequest
Mutipart/form-data with HttpRequest
正确创建multipart / form-data请求的方式。我用了几次这个解决方案,效果很好
is-a?