Find your content:

Search form

You are here

how to make HTTP request for an example

 
Share

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

Attribution to: Pavel

Possible Suggestion/Solution #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.)


Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33226

My Block Status

My Block Content