Guzzle post 嵌套数组问题
Guzzle post nested array Issue
我使用 Laravel 5.5 和 Guzzle 6.3 开发网站。
我在尝试使用 API 创建文件夹到 BOX 时尝试使用 post 嵌套数组时遇到问题。
$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 工作正常。
我也可以使用多部分请求嵌套数组上传。
当嵌套数组可以很好地处理多部分请求时,我不知道为什么我会遇到这个嵌套数组问题。
框文档参考 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
所以也许在创建客户端实例时尝试设置 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
]
]
]);
实际上在再次阅读框参考后,post 没有文件上传的请求它接受 application/json,
这是 form_params 用于 application/x-www-form-urlencoded
供您对包含嵌套字段的数据进行任何 http 请求;您必须在 headers 中包含 Content-Type
;然后像这样将其设置为 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
]
]
]);
我使用 Laravel 5.5 和 Guzzle 6.3 开发网站。
我在尝试使用 API 创建文件夹到 BOX 时尝试使用 post 嵌套数组时遇到问题。
$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 工作正常。
我也可以使用多部分请求嵌套数组上传。
当嵌套数组可以很好地处理多部分请求时,我不知道为什么我会遇到这个嵌套数组问题。
框文档参考 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
所以也许在创建客户端实例时尝试设置 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
]
]
]);
实际上在再次阅读框参考后,post 没有文件上传的请求它接受 application/json, 这是 form_params 用于 application/x-www-form-urlencoded
供您对包含嵌套字段的数据进行任何 http 请求;您必须在 headers 中包含 Content-Type
;然后像这样将其设置为 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
]
]
]);