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
Ega RakeshEga Rakesh 

Chatter Profile photo upload using REST API

HI ,

i am trying to upload profile photo from one salesforce org to other salesforce org using Chatter REST API.

in source org the photo is in Document object. for this i am constructing a multi-part request in Apex. but i am getting an error saying
Bad Request and message as "you have sent us an Illegal URL or an improperly formatted request"

this is my code 

document d = [select body from document where name='samplename.jpg'];
  //uploading profile photo to user profile - multipart
  string size = string.valueof(d.body.size());
  req.setHeader('Authorization','OAuth ' + token.access_token);
  req.setHeader('Content-Type','multipart/form-data; boundary=-----------------------------153501500631101');
  req.setHeader('Host','ap1.salesforce.com');
  req.setHeader('Content-Length', size);
  req.setHeader('Accept','application/json');
  req.setMethod('POST'); 
  string endpoint1 = token.instance_url + '/services/data/v26.0/chatter/users/00590000002s0blAAA/photo';
  req.setEndpoint(endPoint1);
  String body = '-----------------------------153501500631101\r\n';
  body = body+ 'Content-Disposition: form-data; name="json"\r\n';
  body = body + 'Content-Type: application/json; charset=UTF-8\r\n\r\n';
  body = body + '{"cropX" : "0","cropY" : "0","cropSize" : "200"}\r\n\r\n';
  body = body + '-----------------------------153501500631101\r\n';
  body = body + 'Content-Disposition: form-data; name="Filedata"; filename="samplename.jpg"\r\n';
  body = body + 'Content-Type: application/octet-stream; charset=ISO-8859-1\r\n\r\n';
  //body = body + 'Content-transfer-encoding: binary\r\n\r\n';
  body = body + '-----------------------------153501500631101--';
  req.setBody(body); 
  res = h.send(req);

am i doing any mistake here . below is the image which contians the request body constructed. plesae help me i am trying to do this from 2 days 

User-added image


Thanks,
Rakesh