2015-04-03 41 views
0
Dim url As String = String.Format("{0}folders/{1}", boxApiUrl, ParentFolderId) 'ParentFolderId being pass is "0" 
    Using request = New HttpRequestMessage() With {.RequestUri = New Uri(url), .Method = HttpMethod.Post} 
     request.Headers.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", "Bearer " + acctoken) 

     Dim data As Dictionary(Of [String], [String]) = New Dictionary(Of String, String)() 
     data.Add("name", FolderName) 

     Dim content As HttpContent = New FormUrlEncodedContent(data) 
     request.Content = content 
     Dim response = _httpClient.SendAsync(request).Result 
     If response.IsSuccessStatusCode Then 
       '   
     End If 
    End Using 

我懷疑數據沒有正確放在一起,但無法弄清楚如何傳遞文件夾名稱以在根目錄下創建。所有其他功能(讀取根文件夾,上傳文件等)使用令牌工作正常。Box.Net創建文件夾錯誤請求400

回答

1

父文件夾ID在POST正文中傳遞,而不是在URL中傳遞。正文應該是以下格式的JSON數據:{ "name": "FolderName", "parent": { "id": "ParentFolderId" }}Documentation

Dim url As String = String.Format("{0}folders", boxApiUrl) 
Using request = New HttpRequestMessage() With {.RequestUri = New Uri(url), .Method = HttpMethod.Post} 
    request.Headers.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", "Bearer " + acctoken) 

    Dim format as String = @"{{ ""name"":""{0}"", ""parent"": {{ ""id"":""{1}"" }} }}"; 
    Dim body as String = String.Format(format, FolderName, ParentFolderId); 
    request.Content = New StringContent(body, Encoding.UTF8, "application/json") 

    Dim response = _httpClient.SendAsync(request).Result 
    If response.IsSuccessStatusCode Then 
      '   
    End If 
End Using 

順便說一句,你可以使用Json.NET的JsonConvert.SerializeObject方法序列化一個匿名或靜態類型的JSON字符串:

Dim obj = New With {Key .name = FolderName, 
         .parent = New With {Key .id = ParentFolderId }}; 
Dim body as String = JsonConvert.SerializeObject(body); 
request.Content = New StringContent(body, Encoding.UTF8, "application/json")