下面是一个将文件上传到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响应。
我使用的代码有什么问题吗?
发布于 2020-01-14 20:46:03
您的$options数组结构看起来不太正确。
默认情况下,Drupal8对HTTP请求使用Guzzle库。Guzzle文档中有一个关于构建多部分表单数据的部分:http://docs.guzzlephp.org/en/stable/quickstart.html#sending-form-files
而且您的属性在第一个示例中看起来像是JSON编码的,所以您可能希望使用json_encode($attributes)而不是原始数组。
发布于 2021-02-26 18:46:09
我也遇到过类似的问题。解决方案是不在请求上手动设置Content-Type。
PR在文档中添加注释:https://github.com/guzzle/guzzle/pull/2860
https://stackoverflow.com/questions/59707090
复制相似问题