symfonycurlhttp-status-code-422symfony-http-client

Posting with Symfony 4.4 HTTP Client not working, status code 422


I want to post info to external api but I get error 422 all the time. Geting info and authorization works fine. I'm using Symfony Http Client, authorization and headers are defined in framework.yaml for now.

Api documentation fragment:

curl "https://business.untappd.com/api/v1/locations/3/custom_menus"
-X POST
-H "Authorization: Basic bmlja0BuZXh0Z2xhc3MuY286OW5kTWZESEJGcWJKeTJXdDlCeC0="
-H "Content-Type: application/json"
-d '{ "custom_menu": { "name": "Wine selection" } }'

My service fragment:

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']],
                ],
        ]
    );

Solution

  • Try to encode json "manualy", before send. Just like that

    //$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,
        ]
    );