0

I am using this for sending mail with attachment using sendgrid api in salesforce...i am getting error response as bad request and status code:400

anybody help me to figure it out..???

String Boundary = '----------------------------400f182a9360';       
String requestBody;
requestBody ='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="api_user"'+'\r\n\r\n'+'youremail'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="api_key"'+'\r\n\r\n'+'yourpassword'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="to"'+'\r\n\r\n'+'xxx@gmail.com'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="subject"'+'\r\n\r\n'+'test'+'\r\n\r\n' ;
//requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="html"'+'\r\n\r\n'+'<b>hi</b>'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="text"'+'\r\n\r\n'+'mail'+'\r\n\r\n' ;
requestBody +='--' + Boundary +'\r\n\r\n'+'Content-Disposition: form-data; name="from"'+'\r\n\r\n'+'yyy@gmail.com'+'\r\n\r\n' ;
requestBody +=  '--' + Boundary + '\r\n\r\n'+'Content-Disposition: form-data; name="files['+mails.filename+']";filename="'+mails.filename+ '"\r\n\r\n'+ 'Content-Type:'+mails.conType+'\r\n\r\n'+Encodingutil.base64Encode(mails.bl)+'\r\n\r\n';
requestBody+=   '--' + Boundary + '--\r\n';

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();
}

output of this is

------------------------------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--
tomlogic
  • 11,489
  • 3
  • 33
  • 59
user2034816
  • 97
  • 2
  • 2
  • 8

1 Answers1

1

There's a lot in your code that doesn't make sense to me, especially the section for when you're special-casing the base64-encoded data if it ends with =. I'm not sure why you need that. The header and footer appear in the body as straight text, without any extra processing.

One guess would be that you need to be using bodyBlob.toString() instead of just bodyBlob when building up your body (calling that variable url is confusing).

This StackOverflow question has a good example of what the body should look like if you're posting binary data. I have a feeling that you're not building yours up correctly.

And here's another good resource on what the body looks like for posted data. You don't appear to be building something with that format.

Community
  • 1
  • 1
tomlogic
  • 11,489
  • 3
  • 33
  • 59
  • thanks for replying mate....bodyBlob.toString() only works for text files not for other formats like image...what should i do from them....can you help out... – user2034816 Feb 13 '13 at 04:50
  • I'm not that familiar with the `sendgrid` API, but the first thing you need to fix is a double CRLF after the boundary. It should only have one newline, then the headers, then two lines and the data. I also think you can reduce to a single newline between the data and the next boundary. – tomlogic Feb 13 '13 at 18:34