正在上载文件rest api..



我在salesforce中使用sendgrid api发送带有附件的邮件。。。我得到错误响应为错误请求和状态代码:400

有人帮我弄清楚吗。。???

String Boundary = '----------------------------400f182a9360';       
String requestBody;
requestBody ='--' + Boundary +'rnrn'+'Content-Disposition: form-data; name="api_user"'+'rnrn'+'youremail'+'rnrn' ;
requestBody +='--' + Boundary +'rnrn'+'Content-Disposition: form-data; name="api_key"'+'rnrn'+'yourpassword'+'rnrn' ;
requestBody +='--' + Boundary +'rnrn'+'Content-Disposition: form-data; name="to"'+'rnrn'+'xxx@gmail.com'+'rnrn' ;
requestBody +='--' + Boundary +'rnrn'+'Content-Disposition: form-data; name="subject"'+'rnrn'+'test'+'rnrn' ;
//requestBody +='--' + Boundary +'rnrn'+'Content-Disposition: form-data; name="html"'+'rnrn'+'<b>hi</b>'+'rnrn' ;
requestBody +='--' + Boundary +'rnrn'+'Content-Disposition: form-data; name="text"'+'rnrn'+'mail'+'rnrn' ;
requestBody +='--' + Boundary +'rnrn'+'Content-Disposition: form-data; name="from"'+'rnrn'+'yyy@gmail.com'+'rnrn' ;
requestBody +=  '--' + Boundary + 'rnrn'+'Content-Disposition: form-data; name="files['+mails.filename+']";filename="'+mails.filename+ '"rnrn'+ 'Content-Type:'+mails.conType+'rnrn'+Encodingutil.base64Encode(mails.bl)+'rnrn';
requestBody+=   '--' + Boundary + '--rn';
String url= 'https://sendgrid.com/api/mail.send.json';  
Http h = new Http();         
HttpRequest req = new HttpRequest();       
// req.setTimeout(60000);          
req.setEndpoint(url); 
req.setMethod('GET');        
req.setHeader('Content-Type', 'multipart/form-data; boundary=' +Boundary);                  
req.setHeader('Content-Length',String.valueof(requestBody.length()));      
 system.debug('=====length======'+String.valueof(requestBody.length()));
//req.setHeader('Authorization','Bearer '+ssId); 
req.setBody(requestBody);   
HttpResponse res;              
String resURL;            
res = h.send(req);
 response=res.getbody();
System.debug('++++++++++++++'+requestBody);
System.debug('++++++++++++++'+url);
system.debug('=====response is======'+response);
if(response.contains('success'))
{
    response='{"message":"success"}';
}
else
{
    response='error: '+res.getbody();
}

这是的输出

------------------------------400f182a9360
Content-Disposition: form-data; name="api_user"
sendgridusername
------------------------------400f182a9360
Content-Disposition: form-data; name="api_key"
sendgridpassword
------------------------------400f182a9360
Content-Disposition: form-data; name="to"
xxx@gmail.com
------------------------------400f182a9360
Content-Disposition: form-data; name="subject"
test
------------------------------400f182a9360
Content-Disposition: form-data; name="text"
mail
------------------------------400f182a9360
Content-Disposition: form-data; name="from"
yyy@gmail.com
------------------------------400f182a9360
Content-Disposition: form-data; name="files[New Text Document.txt]";filename="New Text Document.txt"
Content-Type:text/plain
YW5pbWVzaCBzZGYgc2RmIHMgZiBzIGRmIHNk
------------------------------400f182a9360--

您的代码中有很多内容对我来说没有意义,尤其是当您对base64编码的数据进行特殊大小写时(如果它以=结尾)的部分。我不知道你为什么需要这个。页眉和页脚在正文中显示为纯文本,无需任何额外处理。

一种猜测是,在构建身体时,您需要使用bodyBlob.toString()而不仅仅是bodyBlob(调用变量url会令人困惑)。

这个StackOverflow问题提供了一个很好的例子,说明如果发布二进制数据,正文应该是什么样子。我有一种感觉,你没有正确地建立自己的基础。

这里还有另一个关于发布数据的正文的好资源。你似乎没有用那种格式构建东西。

相关内容

  • 没有找到相关文章

最新更新