使用 Symfony 4.4 HTTP 客户端发布不工作,状态代码 422
Posting with Symfony 4.4 HTTP Client not working, status code 422
我想 post 向外部 api 发送信息,但我一直收到错误 422。获取信息和授权工作正常。我正在使用 Symfony Http 客户端,授权和 headers 目前在 framework.yaml 中定义。
Api 文档片段:
curl "https://business.untappd.com/api/v1/locations/3/custom_menus"
-X POST
-H "授权:基本 bmlja0BuZXh0Z2xhc3MuY286OW5kTWZESEJGcWJKeTJXdDlCeC0="
-H "Content-Type: application/json"
-d '{ "custom_menu": { "name": "葡萄酒选择" } }'
我的服务片段:
public function customMenu(): int
{
$response = $this->client->request(
'POST',
'https://business.untappd.com/api/v1/locations/'.$this->getLocationId().'/custom_menus',
[
'json' => [
['custom_menu' => ['name' => 'Wine selection']],
],
]
);
有一个错误和一些您没有包含的缺失参数:
- 最重要的:授权(如果您没有将其添加到 'http_client' 参数下的 framework.yaml 文件)。
- 您的 link 以 'custom_menus' 而非 'menus' 结尾。
- 内容类型。
您可以测试此更正:
public function customMenu(): int
{
$response = $this->client->request(
'POST',
'https://business.untappd.com/api/v1/locations/'.$this->getLocationId().'/custom_menus', //corrected '/custom_menus'
[
'auth_basic' => ['bmlja0BuZXh0Z2xhc3MuY286OW5kTWZESEJGcWJKeTJXdDlCeC0='], // To add
'headers' => [ // To add
'Content-Type' => 'application/json',
],
'json' => [
['custom_menu' => ['name' => 'Wine selection']],
],
]
);
// .....
}
尝试在发送前“手动”编码json。就这样
//$jsonData = '{ "custom_menu": { "name": "Wine selection" } }';
$jsonData = json_encode(["custom_menu" => ["name" => "Wine selection"]]);
$response = $client->request(
'POST',
'https://business.untappd.com/api/v1/locations/'.$this->getLocationId().'/custom_menus',
[
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $jsonData,
]
);
我想 post 向外部 api 发送信息,但我一直收到错误 422。获取信息和授权工作正常。我正在使用 Symfony Http 客户端,授权和 headers 目前在 framework.yaml 中定义。
Api 文档片段:
curl "https://business.untappd.com/api/v1/locations/3/custom_menus"
-X POST
-H "授权:基本 bmlja0BuZXh0Z2xhc3MuY286OW5kTWZESEJGcWJKeTJXdDlCeC0="
-H "Content-Type: application/json"
-d '{ "custom_menu": { "name": "葡萄酒选择" } }'
我的服务片段:
public function customMenu(): int
{
$response = $this->client->request(
'POST',
'https://business.untappd.com/api/v1/locations/'.$this->getLocationId().'/custom_menus',
[
'json' => [
['custom_menu' => ['name' => 'Wine selection']],
],
]
);
有一个错误和一些您没有包含的缺失参数:
- 最重要的:授权(如果您没有将其添加到 'http_client' 参数下的 framework.yaml 文件)。
- 您的 link 以 'custom_menus' 而非 'menus' 结尾。
- 内容类型。
您可以测试此更正:
public function customMenu(): int
{
$response = $this->client->request(
'POST',
'https://business.untappd.com/api/v1/locations/'.$this->getLocationId().'/custom_menus', //corrected '/custom_menus'
[
'auth_basic' => ['bmlja0BuZXh0Z2xhc3MuY286OW5kTWZESEJGcWJKeTJXdDlCeC0='], // To add
'headers' => [ // To add
'Content-Type' => 'application/json',
],
'json' => [
['custom_menu' => ['name' => 'Wine selection']],
],
]
);
// .....
}
尝试在发送前“手动”编码json。就这样
//$jsonData = '{ "custom_menu": { "name": "Wine selection" } }';
$jsonData = json_encode(["custom_menu" => ["name" => "Wine selection"]]);
$response = $client->request(
'POST',
'https://business.untappd.com/api/v1/locations/'.$this->getLocationId().'/custom_menus',
[
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $jsonData,
]
);