1

我想将信息发布到外部 api,但我一直收到错误 422。获取信息和授权工作正常。我正在使用 Symfony Http 客户端,授权和标头现在在 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']],
                ],
        ]
    );
4

2 回答 2

0

有一个错误和一些您没有包含的缺失参数:

  • 最重要的:授权(如果您没有将其添加到您的 framework.yaml 文件中的“http_client”参数下)。
  • 您的链接以“cu​​stom_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']],
            ],
        ]
    );

    // .....
}
于 2021-03-28T12:08:24.383 回答
0

在发送之前尝试“手动”编码 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,
    ]
);
于 2021-03-30T04:34:49.023 回答