API upload did not contain a file part
Hello there
I am trying to upload text files into Box directly from Salesforce API callouts.
When I try to call your API endpoint, I get a 400 error with the message "API upload did not contain a file part".
here is my code ,
public static List<String> FileSubmission(Id OpportunityId)
{
string authToken=GetAuthToken();
set<id> cdId=new set<id>();
List<ContentDocumentLink> cdl=[SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId =: OpportunityId];
if(cdl.size()>0){
for(ContentDocumentLink cdlList:cdl)
{
cdId.add(cdlList.ContentDocumentId);
}
}
List<ContentVersion> cv=[SELECT Title,VersionData,FileType FROM ContentVersion WHERE ContentDocumentId IN: cdId order by CreatedDate desc limit 1];
String fileName=cv[0].Title;
system.debug(cv[0].Title);
List<string> str=new List<string>();
str.add(fileName);
System.JSONGenerator gen = System.JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('name', fileName);
gen.writeFieldName('parent');
gen.writeStartObject();
gen.writeStringField('id','0');
gen.writeEndObject();
gen.writeEndObject();
String jsonbody = gen.getAsString();
if(cv.size()>0){
String targetURL ='https://upload.box.com/api/2.0/files/content';
String separationString = '--------------------------363928481663217658578398';
string fileContent = EncodingUtil.base64Encode(cv[0].VersionData);
String bodyPayload = '\n\n'+separationString + '\n Content-Disposition: form-data; name="attributes"\n\n'+jsonbody+'\n'+separationString+'\n content-disposition: form-data; name:"files"; filename="'+fileName+'"\n content-type: text/plain; \n\n'+fileContent+'\n'+separationString;
System.debug('bodyPayload: '+bodyPayload);
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'multipart/form-data; boundary='+separationString);
req.setHeader('Content-Length', String.valueOf(fileContent.length()));
req.setHeader('Authorization', 'Bearer '+authToken);
req.setMethod('POST');
req.setEndpoint(targetURL);
req.setBody(bodyPayload);
system.debug('AuthToken-------------------' +authToken);
HttpResponse response = http.send(req);
system.debug('getError ----'+ response.getBody()) ;
system.debug('response'+response);
if(response.getStatusCode() == 200){
system.debug('success');
return str;
}
}
return null;
}
here is my response, with picture,
access token = yVKXzm1Zas78Zz67q7lUDcJ4tNYAF8v
file name = SampleFileTEXTNOTEPAD
"code":"bad_request","help_url":"http://developers.box.com/docs/#errors","status":400,"message":"API upload did not contain a file part","type":"error"
投稿コメントは受け付けていません。
コメント
0件のコメント