Box.Net创建文件夹错误请求400

时间:2015-04-03 07:04:02

标签: box-api

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

我怀疑数据没有正确放在一起,但是无法弄清楚如何在根目录下传递要创建的文件夹名称。使用令牌的所有其他功能(读取根文件夹,上传文件等)工作正常。

1 个答案:

答案 0 :(得分: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")