user3607700
user3607700

Reputation: 1

Box.com create/update folder from salesforce using API v2

I am trying to create a new folder in the Box from a controller class in Salesforce using the API version 2. I am receiving the access token and I am also been able to retrieve the items of a folder with a HTTP GET request. But I am not able to create a new folder in BOX. Also not able to copy files from 1 folder to another or update information about a folder. Below is the code to update the Description of my folder:

 Http h = new Http();
        HttpRequest req = new HttpRequest();
        string endPointValue = 'https://api.box.com/2.0/folders/myfolder_id';
        req.setEndpoint(endPointValue);
        req.setHeader('Authorization', 'Bearer ' + myaccessToken);
        req.setBody('description=' + EncodingUtil.urlEncode('New', 'U`enter code here`TF-8'));       
        req.setMethod('POST');
        HttpResponse res = h.send(req); 

I am getting the following response:

{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid value 'description=New'. Entity body should be a correctly nested resource attribute name/value pair"}]},"help_url":"http://developers.box.com/docs/#errors","message":"Bad Request","request_id":"my request Id"}

Can anyone help me on this?

Upvotes: 0

Views: 493

Answers (1)

IvanR
IvanR

Reputation: 567

According to documentation here, Box API expects request parameters in JSON format and request method has to be PUT. Try following:

Http h = new Http();
HttpRequest req = new HttpRequest();
string endPointValue = 'https://api.box.com/2.0/folders/myfolder_id';
req.setEndpoint(endPointValue);
req.setHeader('Authorization', 'Bearer ' + myaccessToken);
req.setBody('{"description" : "New folder description"}');       
req.setMethod('PUT');
HttpResponse res = h.send(req); 

P.S. you were also using EncodingUtil.urlEncode() method incorrectly. First parameter should be a string you are trying to make URL-safe and second parameter is encoding (see documentation here)

Upvotes: 1

Related Questions