Apexでboundary複数でかつファイルを含むmultipartリクエスト
ちょっと汚いけど下記でできる。
//ファイルの添付
public static void regFile(String token, String docId) {
Attachment objPDF = [SELECT id, Body, Name FROM Attachment WHERE id = 'AAAAAAAAAAAA'];
String endpoint = 'https://XXXXXXXXXXXXXXXXXXXXXXXXX';
String boundary = String.valueOf(DateTime.now().getTime());
String strSeparationKey = 'A_RANDOM_STRING';
String body = '';
body+='------------' + strSeparationKey + '\n';
body+='Content-Disposition: form-data; name="test"\n';
body+='\n';
body+='aiueo' + '\n';
body+='------------' + strSeparationKey + '\n';
// assemble the body payload
String strHeader = body + '--' + strSeparationKey + '\nContent-Disposition: form-data; name="uploadfile"; filename="' + objPDF.Name + '"\nContent-Type: application/octet-stream\n\n';
String strBody = EncodingUtil.base64Encode(objPDF.Body);
String strFooter = '\n--' + strSeparationKey + '--';
String strHeaderEncoded = EncodingUtil.base64Encode(Blob.valueOf(strHeader+'\n'));
while(strHeaderEncoded.endsWith('=')) {
strHeader+=' ';
strHeaderEncoded = EncodingUtil.base64Encode(Blob.valueOf(strHeader+'\n'));
}
String strBodyEncoded = strBody;
String strFooterEncoded = EncodingUtil.base64Encode(Blob.valueOf(strFooter));
Blob blobBody = null;
String last4Bytes = strBodyEncoded.substring(strBodyEncoded.length()-4,strBodyEncoded.length());
if(last4Bytes.endsWith('=')) {
Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
HttpRequest objHttpRequest = New HttpRequest();
objHttpRequest.setBodyAsBlob(decoded4Bytes);
String last4BytesFooter = objHttpRequest.getBody()+strFooter;
blobBody = EncodingUtil.base64Decode(strHeaderEncoded+strBodyEncoded.substring(0,strBodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
} else {
blobBody = EncodingUtil.base64Decode(strHeaderEncoded+strBodyEncoded+strFooterEncoded);
}
if(blobBody.size()>3000000) {
// throw new CustomException('File size limit is 3 MBytes');
system.debug('File size limit is 3 MBytes');
}else{
system.debug('blobBody.size()'+blobBody.size());
}
// send out the request
HttpRequest req = New HttpRequest();
req.setHeader('Content-Type', 'multipart/form-data; boundary=' + strSeparationKey);
req.setHeader('Authorization', 'Bearer ' + token);
req.setMethod('POST');
req.setEndpoint(endpoint);
req.setBodyAsBlob(blobBody);
req.setHeader('Content-Length', String.valueof(req.getBodyAsBlob().size()));
Http http = New Http();
HTTPResponse res = http.send(req);
system.debug('res'+res.getBody());
}
base64対応だとまだ楽
public static void regFile(String token, String docId) {
Attachment atch = [SELECT id, Body FROM Attachment WHERE id = 'AAAAAAAA'];
String endpoint = 'https://AAAAAAAAAAAAAAAAAAA' + docId + '/files';
HttpRequest request = new HttpRequest();
request.setHeader('Authorization', 'Bearer ' + token);
request.setEndpoint(endpoint);
request.setMethod('POST');
request.setHeader('Accept-Language', 'ja');
request.setHeader('accept', 'application/json');
String boundary = String.valueOf(DateTime.now().getTime());
String body = '';
body+='------------' + boundary + '\r\n';
body+='Content-Disposition: form-data; name="name"\r\n';
body+='\r\n';
body+='aiueo' + '\r\n';
body+='------------' + boundary + '\r\n';
body+='Content-Disposition: form-data; name="uploadfile"; filename="test.pdf"\r\n';
body+='Content-Transfer-Encoding: base64\r\n';
String contentType = 'application/octet-stream';
body+='Content-Type: ' + contentType + '\r\n\r\n';
body+=EncodingUtil.base64Encode(atch.body);
body+='\r\n------------' + boundary + '--';
request.setHeader('Content-Type', 'multipart/form-data; boundary=----------' + boundary);
request.setHeader('Content-Length',String.valueof(body.length()));
request.setBody(body);
System.debug(request.getBody());
Http http = new Http();
HttpResponse response = http.send(request);
response.getBody();
}