首页 > 解决方案 > 文件从 Drupal 上传到 Box

问题描述

下面是一个将文件上传到 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

我正在尝试在代码中做同样的事情:

$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);

但我从 Box 收到了 400 Bad Request 响应。

关于我正在使用的代码有什么问题的任何想法?

标签: drupal-8

解决方案


我遇到了类似的问题。解决方案是不在请求上手动设置 Content-Type。

PR 向文档添加注释:https ://github.com/guzzle/guzzle/pull/2860


推荐阅读