我使用 Laravel 5.5 和 Guzzle 6.3 开发网站。
我在尝试使用 API 为 BOX 创建文件夹时尝试大量发布嵌套数组时遇到问题。
$url = $this->api_url . "/folders";
$headers = [
'Authorization' => 'Bearer ' . $this->access_token,
];
$client = new Client();
$response = $client->post($url, [
'headers' => $headers,
'form_params' => [
'name' => $name,
'parent' => [
'id' => $parent_id
]
]
]);
它向我显示了这样的错误:Entity body should be a correctly nested resource attribute name/value pair
我也已经尝试使用
shell_exec
curl 所以它从命令提示符运行 curl 它给了我同样的错误但是当我尝试从 cygwin 运行时,curl 工作正常。
我也可以使用多部分请求嵌套数组进行上传工作正常。
当嵌套数组在多部分请求中正常工作时,我不知道为什么会遇到此嵌套数组问题。
Box 文档 POST 的引用 is here .
最佳答案
根据docs您不能使用 multipart 选项:
form_params cannot be used with the multipart option. You will need to use one or the other. Use form_params for application/x-www-form-urlencoded requests, and multipart for multipart/form-data requests.
This option cannot be used with body, multipart, or json
因此,也许可以尝试在创建 Client 实例时设置 header :
$url = $this->api_url . "/folders";
$client = new Client([
'headers' => [
'Authorization' => 'Bearer ' . $this->access_token,
'Accept' => 'application/json',
]
]);
$response = $client->post($url, [
'json' => [
'name' => $name,
'parent' => [
'id' => $parent_id
]
]
]);
实际上在再次阅读框引用后,没有文件上传的发布请求接受 application/json ,这是用于 application/x-www-form-urlencoded 的 form_params
关于laravel - Guzzle 发布嵌套数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52411226/