You need to sign in to do that
Don't have an account?
onedrive rest api call to upload a file...
hi i am having trouble with calling onedrive api.here is my code,i dont understand how to use boundary in this.i am a newbie with api and going with the onedrive documentation.Any help would be appreciated.thanks
public void uploadFile(string accessToken){
set<id> allids = new set<id>();
allids.add('00Q2800000Fwl1b');
List<Attachment> content = [Select Id, Name, ContentType, BodyLength, Body, CreatedById, Description From Attachment Where ParentId IN :allids limit 1];
system.debug('@@content'+content);
httpRequest req = new httpRequest();
req.setEndPoint('https://apis.live.net/v5.0/me/skydrive/files?access_token='+accessToken);
req.setHeader('Content-Disposition', 'form-data; name="file"; filename="content[0].Name"');
req.setHeader('Content-Type', 'multipart/form-data; boundary=A300x');
//req.setLength('Content-Length',string.valueOf(content[0].BodyLength));
string Body = string.valueOf(content[0].Body); req.setBody(Body);
req.setHeader('Content-Length',string.valueOf(content[0].BodyLength));
req.setCompressed(true); req.setMethod('POST');
HttpResponse res = null;
http h= new Http();
res= h.send(req); system.debug('@@@@'+res);
System.debug('---------------------'+res.getbody());
}
error in the debug:
"error": { "code": "request_body_invalid", "message": "The request entity body for multipart form-data POST isn't valid. The expected format is:\u000d\u000a--[boundary]\u000d\u000aContent-Disposition: form-data; name=\"file\"; filename=\"[FileName]\"\u000d\u000aContent-Type: application/octet-stream\u000d\u000a[CR][LF]\u000d\u000a[file contents]\u000d\u000a--[boundary]--[CR][LF]" }
The relavant method is:
So to modify your existing method:
All Answers
The relavant method is:
So to modify your existing method:
thanks for the reply Daniel.. it worked.
here is my code..
public void uploadFile(string accessToken){
set<id> allids = new set<id>();
allids.add('00Q2803450Fwl1b');
List<Attachment> content = [Select Id, Name, ContentType, BodyLength, Body, CreatedById, Description From Attachment Where ParentId IN :allids limit 1];
system.debug('@@content'+content);
Blob file_body = content[0].Body;
String file_name = content[0].Name;
String reqEndPoint = 'https://apis.live.net/v5.0/me/skydrive/files?access_token='+accessToken;
String boundary = '----------------------------741e90d31eff';
String header = '--'+boundary+'\r\nContent-Disposition: form-data; name="file"; filename="'+content[0].Name+'"\r\nContent-Type: application/octet-stream'; // added '\r's removed ';' see Tim Smith's comment
String footer = '\r\n--'+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);
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
Blob bodyBlob = null;
String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
if(last4Bytes.endsWith('='))
{
Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
HttpRequest tmp = new HttpRequest();
tmp.setBodyAsBlob(decoded4Bytes);
String last4BytesFooter = tmp.getBody()+footer;
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.substring(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
}
else
{
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
req.setMethod('POST');
req.setEndpoint(reqEndPoint);
req.setBodyAsBlob(bodyBlob);
//req.setTimeout(120000);
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug('@@@@'+res);
System.debug('---------------------'+res.getbody());
}
hi Daniel,
i am having one more problem.i am getting HTTP Error 400. The request is badly formed when try to get the authentication code .
its working perfectly when i call from my browser.