Httprequest错误。帮助提出正确的请求

时间:2014-04-21 12:05:20

标签: salesforce httprequest box-api

我有示例请求:

curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" \
-F filename=@FILE_NAME \
-F parent_id=PARENT_FOLDER_ID

我的请求会返回错误missing_parameter"," name":" parent"," message":"' parent&# 39;是必需的"。

这是我的要求:

    String boundary = String.valueOf(DateTime.now().getTime());
    String body = '------------' + boundary + '\r\n';
    body +='Content-Disposition: form-data; name="FileName"; FileName="' + fileBody +'"\r\n';
    body+='Content-Transfer-Encoding: base64\r\n';
    if ((contentType == null) || (contentType == '')){contentType = 'application/octet-stream';}
    body+='Content-Type: ' + contentType + '\r\n\r\n';
    body+=EncodingUtil.base64Encode(fileBody);
    body +='Content-Disposition: form-data; name="Parent"; \r\n';
    body+='Content-Transfer-Encoding: base64\r\n';
    if ((contentType == null) || (contentType == '')){contentType = 'application/octet-stream';}
    body+='Content-Type: ' + contentType + '\r\n\r\n';
    body+=0;

    body+='\r\n------------' + boundary + '--'; 

    HttpRequest req = new HttpRequest();

    req.setHeader('Content-Type', 'multipart/form-data; boundary=----------' + boundary);
    req.setHeader('Content-Length',String.valueof(body.length()));

    req.setBody(body);
    req.setHeader('Authorization', 'Bearer ' + accessToken.token__c);
    req.setMethod('POST');
    req.setEndpoint('https://upload.box.com/api/2.0/files/content');

    Http http = new Http();
    HttpResponse res = http.send(req);

1 个答案:

答案 0 :(得分:0)

要求您上传的文件夹的ID。

尝试更改此行:

body +='Content-Disposition: form-data; name="Parent"; \r\n';

为:

body +='Content-Disposition: form-data; name="parent_id"; \r\n';

编辑:虽然我不得不问,为什么要将整个文件正文作为文件名发送?