0

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?

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.