How to encode a PDF to send from Salesforce Apex to Box vía API?
Hello, i'm trying to send a pdf file via Apex HttpRequest but i dont know how to convert the pdf file into a codec for send it via POST Request to Box, i already try to send the doc as a "Blob" string but i doesnt work, any idea of how to encode Pdf file to send it to Box vía API Http Request POST?
Here's my code:
Pagereference pdf = Page.InvoiceTest_Visualforce;
pdf.getParameters().put('id', 'some_id');
Blob body = pdf.getContentAsPDF();
String separationString = '--boundary123';
String header = '--' + separationString + '\r\nContent-Disposition: form-data; name="attributes"\r\n\r\n{"name":"TesApex11.pdf", "parent":{"id":"0"}}\r\n'+
'--' + separationString + '\r\nContent-Disposition: form-data; name="file"; filename="DocumentTest.pdf"'+
'\r\nContent-Type: "application/pdf"\r\n\r\n';
String bodyTo = body +'\r\n';
String footer = '--' + separationString + '--';
HttpRequest req = new HttpRequest();
req.setEndpoint('https://upload.box.com/api/2.0/files/content');
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer token_here');
//req.setHeader('Content-Type', 'multipart/form-data');
req.setHeader('Content-Type', 'multipart/form-data; boundary=' + separationString);
String cuenta = String.valueof(bodyPayload.replaceAll('\n', ''));
req.setHeader('Content-Length', cuenta);
req.setBody(bodyPayload);
req.setCompressed(false);
System.debug('BodyPayload: '+ bodyPayload);
Note: i already send a file to box but for the wrong encode the file is not correctly formed and i when i try to see it from box or some pdf reader its show an: "error, not valid archive"
Please sign in to leave a comment.
Comments
0 comments