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 Answer 1

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.)

You must log in to answer this question.

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