2

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']],
                ],
        ]
    );
dofado
  • 25
  • 6

2 Answers2

0

There are an error and some missing parameters you didn't include:

  • The most important : Authorization (if you didn't add it to your framework.yaml file under 'http_client' parameter).
  • Your link ends with 'custom_menus' not 'menus'.
  • The content-type.

You can test this correction:

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

    // .....
}
MustaphaC
  • 11
  • 2
  • 1
    As I wrote in my question, content type and auth is defined in framework.yaml. I tested authorization and getting information form api- that works. For safety i tested adding auth and headers to service itself before posting question, and that didn't change anything. Yeah, my url here is wrong, edited my question to proper one but that is not where problem is. I tried doing same with other urls from documentation and problem persists. – dofado Mar 28 '21 at 12:52
0

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,
    ]
);
Emilian
  • 80
  • 4