Box.Net 创建文件夹错误请求 400
Box.Net Create Folder Bad Request 400
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
我怀疑数据没有正确地放在一起,但无法弄清楚如何传递要在根目录下创建的文件夹名称。使用令牌的所有其他功能(读取根文件夹、上传文件等)都可以正常工作。
父文件夹 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")
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
我怀疑数据没有正确地放在一起,但无法弄清楚如何传递要在根目录下创建的文件夹名称。使用令牌的所有其他功能(读取根文件夹、上传文件等)都可以正常工作。
父文件夹 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")