function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prakash GBPrakash GB 

unable to upload file from one s-org to another s-org using REST callout

unable to upload a file using REST callout from one salesforce org to another org. the following JSON parser error i'm getting. could anyone solve the issue.
13:00:59:056 USER_DEBUG [48]|DEBUG|[{"message":"Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:1, column:2]","errorCode":"JSON_PARSER_ERROR"}]

here is the code which i've written.
 
attachment att=[Select Id, Name, Body, ContentType,parentid from attachment where ID = '00P7F000000mF2JUAU'];
blob file_body=att.body;
string file_name=att.name;
String boundary = '--741e90d31eff--';
ContentType="'+att.ContentType+'";\nContent-Type: application/octet-stream';
String header ='--'+boundary+'\nContent-Disposition: form-data; name="entitydocument";\n';
header +='Content-Type : application/json;\n';
header +='{\n"Keywords":"marketing",\n"Name" : "Marketing",\n"parentid" : "0019000000ldtMWAAY",\n "ContentType" : "'+att.ContentType+'"\n}\n--'+boundary+'----';
String footer ='--'+boundary+'--';             
String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
while(headerEncoded.endsWith('='))
{
    header+='  ';
    headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
}
String bodyEncoded = EncodingUtil.base64Encode(file_body);
Blob bodyBlob = null;
String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
if(last4Bytes.endsWith('==')) {
    last4Bytes = last4Bytes.substring(0,2) + '0K';
    bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
    String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
    bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
} else if(last4Bytes.endsWith('=')) {
    last4Bytes = last4Bytes.substring(0,3) + 'N';
    bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
    footer = '\n' + footer;
    String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
    bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);              
} else {
    
    footer = '\r\n' + footer;
    String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
    bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);  
}
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
req.setHeader('accept','application/octet-stream');
req.setMethod('POST');
req.setHeader('Authorization','Bearer 00D7F000000yKem!ARoAQGPBdNrjxwJMHE9Buos1MUd4KwYfxmIb1Hchy8PyolBqzyEeNAV*************************************');

req.setEndpoint('https://ap5.my.salesforce.com/services/data/v40.0/sobjects/Attachment/');
req.setBodyAsBlob(bodyBlob);
req.setTimeout(120000);
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug(res.getbody());
system.debug(header);

thanks.​