Drupal 8 从Drupal将文件上载到Box
下面是一个将文件上载到Box的curl请求Drupal 8 从Drupal将文件上载到Box,drupal-8,Drupal 8,下面是一个将文件上载到Box的curl请求 curl -X POST https://upload.box.com/api/2.0/files/content \ -H "Authorization: Bearer <token>" \ -H "Content-Type: multipart/form-data" \ -F attributes='{"name":"Test.pdf", "parent":{"id":"123"}}' \ -F file=@test.pdf
curl -X POST https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer <token>" \
-H "Content-Type: multipart/form-data" \
-F attributes='{"name":"Test.pdf", "parent":{"id":"123"}}' \
-F file=@test.pdf
但我从盒子里得到了400个错误的请求响应
你知道我使用的代码有什么问题吗?你的$options数组结构看起来不太正确 默认情况下,Drupal8将Guzzle HTTP库用于HTTP请求。Guzzle文档中有一节介绍如何构建多部分表单数据:
在第一个示例中,属性看起来像是JSON编码的,因此您可能希望使用JSON_encode($attributes),而不是原始数组。我遇到了类似的问题。解决方案是不根据请求手动设置内容类型 PR向文档中添加注释:
$contents = file_get_contents($uri);
$attributes = array(
'name' => $filename,
'parent' => array('id' => $folderId),
);
$options = array(
'attributes' => $attributes,
'file' => $contents,
'headers' => array(
'Content-Type' => 'multipart/form-data',
'Authorization' => 'Bearer '. $boxAccess->getToken()
)
);
$uri = 'https://upload.box.com/api/2.0/files/content';
$client = \Drupal::httpClient();
$response = $client->request('POST', $uri, $options);