2014-05-06 56 views
0

我正嘗試使用api版本2在salesforce中的控制器類中創建Box中的新文件夾。我正在接收訪問令牌,並且我也能夠使用HTTP GET請求檢索文件夾的項目。 但我無法在BOX中創建新文件夾。也無法將文件從1個文件夾複製到另一個文件夾或更新有關文件夾的信息。 下面是更新我的文件夾的描述的代碼:Box.com使用api v2從salesforce創建/更新文件夾

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! 

回答

1

根據文檔here,盒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

+0

謝謝伊萬!這工作:) – user3607700

相關問題