我正在尝试使用api版本2从salesforce中的控制器类在Box中创建一个新文件夹。我正在接收访问令牌,我也能够使用HTTP GET请求检索文件夹的项目。 但是我无法在BOX中创建新文件夹。也无法将文件从1个文件夹复制到另一个文件夹或更新有关文件夹的信息。 以下是更新我的文件夹描述的代码:
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?
Thanks in advance!
答案 0 :(得分:1)
根据文档here,Box API需要JSON格式的请求参数,请求方法必须是PUT。请尝试以下方法:
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。你也错误地使用EncodingUtil.urlEncode()
方法。第一个参数应该是您尝试使URL安全的字符串,第二个参数是编码(请参阅文档here)