0

I am unable to send a png file from Salesforce ContentVersion to Microsoft OneDrive. Below is my code:

for (ContentVersion cv : ListFilesToBeUploaded)
{
    Http http = new Http();
    httpRequest req = new httpRequest();
    req.setMethod('PUT');

    String endPoint = 'https://graph.microsoft.com/v1.0/me/drive/root:/' + cv.Title + '.png' + ':/content';
    system.debug('#### endpoint: ' + endPoint);
    req.setEndpoint(endPoint);

    req.setHeader('Authorization', 'bearer ' + accessToken);
    req.setHeader('Content-Type', 'application/octet-stream');

    String bodyDecoded = EncodingUtil.base64Encode(cv.VersionData);
    req.setBody(bodyDecoded);

    httpResponse res = new httpResponse();
    res = http.send(req);
    integer statusCode = res.getStatusCode();
    string resBody = res.getBody();
    system.debug('#### sc: ' + statusCode);
    system.debug('#### rb: ' + resBody);                
}

The error i am receiving is:

HTTP Error 400. The request is badly formed.

I am not able to properly convert the Blob to a format which is valid in OneDrive API.

1 Answer 1

1

Solved it!

The body should be set as: req.setBodyAsBlob(cv.VersionData);

The error was coming as the file name had space in it.

1
  • Mark it as answer!
    – Ayub
    May 16, 2018 at 14:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .