Skip to main content
Question

list of folders in main folder

  • May 21, 2025
  • 9 replies
  • 40 views

Forum|alt.badge.img

Dear all

I want to get folder's info of my main folder in my app.

Here is request:

curl https://api.box.com/2.0/folders/0/items?limit=2&offset=0 \
-H "Authorization: Bearer MY_TOKEN"

but it alway return me 

{"type":"error","status":405,"code":"method_not_allowed","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Method Not Allowed","request_id":"removed for privacy57c569e6b4ac8"} 

json response.

Anything wrong with my request?

Many thanks

 

9 replies

Forum|alt.badge.img

Forum|alt.badge.img

Hi Murtza,

Thank you for your response.

I just started with box, so not very good in it.

Basically what I need is to upload a file to a folder (choose folder by name not by ID) using PHP (curl).

I do not know if I going correct path, but what I done so far is went through

Build a Box Integration tutorials.

I created box application.

Found how to get developers token, refresh token.

 

I was able to create folder

 

$parent = array();
		$parent['id'] = '0';
		$params = array();
		$params['name'] = 'Testfolder';
		$params['parent'] = $parent;

		$params = json_encode($params);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/folders");
		curl_setopt($ch, CURLOPT_HEADER, false); 
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',                                                                                
		    'Content-Length: ' . strlen($params), 'Authorization: Bearer '.$token));
		$result = curl_exec($ch);
		curl_close($ch);
	
		print_r($result);

 

, upload file to main folder or created folder knowing folder's ID.

$url = "https://upload.box.com/api/2.0/files/content";
	
	 $json = json_encode(array(
                                'name' => 'meter.xlsx', 
                                'parent' => array('id' =>'0')// 0 - uploads to main folder
                            ));
         $fields = array(
                      'attributes' => $json,
                      'file'=> new CurlFile('meter.xlsx','application/octet-stream','meter.xlsx')
                  );

       
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL, $url);
	    
   	  
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Authorization: Bearer '.$token
            ));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            $response = curl_exec($ch);
            curl_close($ch);
        

        print_r($response);

but how to upload a file to a folder by name not by ID that is a problem.

The idea was to Get Folder's Items of main folder, then match ID with name in json response received.

 

 


Forum|alt.badge.img

I'm using the Java SDK and I have the same problem with "folder.getInfo()".

 

Using the sample code provided (as below), I also get error 405.

 

    BoxFolder folder = new BoxFolder(api, "candy");
    BoxFolder.Info info = folder.getInfo();

 

I get this error too for "folder.delete()"

 

In both cases when I debug it, it fails when the URL request is sent.


Forum|alt.badge.img

Yes.


Forum|alt.badge.img

Forum|alt.badge.img

The 2nd parameter to BoxFolder in box-java-sdk expects an ID. 

       

BoxFolder boxfolder = new BoxFolder(api, "removed for privacy7");  // api, ID
BoxFolder.Info boxFolderInfo = boxfolder.getInfo();
System.out.println(boxFolderInfo.getName());

 


Forum|alt.badge.img

Oh thanks Ken.

 

How do you get the ID of a folder if you only know its name?

 

I'm going to guess that it's by iterating through all items and looking for the name (?)

 

         for (BoxItem.Info itemInfo : parentFolder)

 

 

PeterD


Forum|alt.badge.img

I think so.  Here's 2 ways I search for something...

Given a BoxFolder, you can search like you mention above...

 

for (BoxItem.Info itemInfo : folder) {
  if (itemInfo instanceof BoxFile.Info) {
     BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
     if (fileName.equals(fileInfo.getName())) {
        fileExists = true;
        fileId = fileInfo.getID();
     }
  }
}

Or use BoxSearch:

BoxSearch boxSearch = new BoxSearch(api);

BoxSearchParameters boxSearchParameters = new BoxSearchParameters("ken");  // search parameter name=ken
boxSearchParameters.setType("folder");                                     // only return folders

List parentFolderId = new ArrayList<>();
parentFolderId.add("0"); // eg.  All Files
boxSearchParameters.setAncestorFolderIds(parentFolderId);

PartialCollection results = boxSearch.searchRange(0, 10, boxSearchParameters);
for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {
   BoxItem.Info info = iterator.next();
   System.out.println(info.getParent().getName() + "-" + info.getName() + " [" + info.getID() + "]");           
}

 

 

 


Forum|alt.badge.img

Thanks Ken, 

 

this was very helpful.

 

regards

PeterD