3

Example:

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

My request is bad:

    String body = 'fileName=' + filebody +'\r\n';
    body += 'parent_id="0"';
    HttpRequest req = new HttpRequest();
    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

When you use curl -F the Content-Type is automatically defaulted to multipart/form-data. I can find no documentation about any defaulting of that header for HttpRequest so one thing to try is to add:

req.setHeader('Content-Type', 'multipart/form-data');

Also the Content-Length header is the size of the content in octets (bytes) which can be a bigger number than the length of the content string in characters. I have a number of examples where I do not set the Content-Length and (I assume) the HttpRequest class is setting it based on the content specified in the setBody call.

(Presumably it was it a 400 HTTP status code that you got.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.