Laravel Guzzle post嵌套数组问题
我使用Laravel5.5和Guzzle 6.3开发网站 在尝试使用API创建文件夹到框时,我在尝试大量使用post嵌套数组时遇到了一个问题Laravel Guzzle post嵌套数组问题,laravel,curl,xdebug,guzzle,Laravel,Curl,Xdebug,Guzzle,我使用Laravel5.5和Guzzle 6.3开发网站 在尝试使用API创建文件夹到框时,我在尝试大量使用post嵌套数组时遇到了一个问题 $url = $this->api_url . "/folders"; $headers = [ 'Authorization' => 'Bearer ' . $this->access_token, ]; $client = new Client(); $response = $client-
$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
]
]
]);
它向我显示了如下错误:
实体主体应该是正确嵌套的资源属性名称/值对
我还尝试过使用shell\u exec
curl,所以它从命令提示符下运行curl,它会给我类似的错误
但当我试着从cygwin那里逃跑时,卷发效果很好
我还可以使用多部分请求嵌套数组进行上传,效果很好
我不知道当嵌套数组可以很好地处理多部分请求时,我为什么会遇到这个嵌套数组问题
box documentation POST的参考。根据您不能使用多部分选项:
form_参数不能与multipart选项一起使用。您需要使用其中一个。对于应用程序/x-www-form-urlencoded请求,使用form_参数;对于多部分/表单数据请求,使用multipart参数
此选项不能与body、multipart或json一起使用
因此,在创建客户端实例时,可以尝试设置标头:
$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
]
]
]);
实际上,在再次读取box引用之后,post请求不带文件上传,它接受application/json,
这是用于应用程序的表单参数/x-www-form-urlencoded,用于对包含嵌套字段的数据进行任何http请求;您必须在标题上包含
内容类型
;然后将其设置为application/x-www-form-urlencoded
,如下所示:
$url = $this->api_url . "/folders";
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Bearer ' . $this->access_token,
];
$client = new Client();
$response = $client->post($url, [
'headers' => $headers,
'form_params' => [
'name' => $name,
'parent' => [
'id' => $parent_id
]
]
]);
是的,我知道form_params不能与multipart选项一起使用,我已经尝试了你提到的,它仍然给了我相同的错误我知道它不需要multipart我只提到,我上传它的代码/我的卷曲也有嵌套数组,它工作得非常好好好吧,我根据你的建议来解决它,我将编辑您的建议,然后可能问题是您正在为父级传递关联数组。可能会对您有所帮助是的,您的第一个建议实际上有一半是正确的,将标题移出以调用guzzle,然后添加应用程序json并将表单参数更改为json谢谢Daniel的帮助!