Can't get webhook's request body in route


I need some help from anyone familiar with webhooks, or Snipcart in particular.
I am trying to follow Snipcart’s documentation for calculating rates with a webhook.

This is the route code the webhook calls: (rates are simplified)

  'pattern' => 'rates',
  'action' => function() {
    $rates = [
      'GR' => [
        'cost' => 10,
        'description' => 'Αποστολή εντός Ελλάδας',
        'guaranteedDaysToDelivery' => 3
      'INT' => [
        'cost' => 20,
        'description' => 'International shipping rate',
        'guaranteedDaysToDelivery' => 10

    $request_data = kirby()->request()->data();
    $content = $request_data['body']['content'];
    $country = null;

    if ($content['shippingAddressSameAsBilling'] === true) {
      $country = $content['billingAddressCountry'];
    } else {
      $country = $content['shippingAddressCountry'];

    $data = [
      'rates' => []
    if ($country === 'GR') {
      array_push($data['rates'], $rates['GR']);
    } else {
      array_push($data['rates'], $rates['INT']);
    return new Response(json_encode($data), 'application/json');
  'method' => 'POST'

The problem is that the route does not seem to receive the request body the webhook is sending.
I am not sure how to test for this, but the $country variable ends up empty.

1 Like

Im not the greatest with this but ive tinkered a bit with this kind of thing. I think the issue is that its one way… you only posting out. I think you need the method to be both post and get.

Normally to debug this kind of stuff you use a tool to start off with to make sure getting the responses correct. Theres a bunch of free ones out there like Postwomen

@jimbobrjames Ι tried setting the method to POST|GET but the same thing happens. I tried the Insomnia tool to send a mock of the body the webhook is sending, and the route handles it correctly.

So I wonder if kirby()->request()->data() the right way to get the webhook’s request body.

This is not correct, $request_data should contain the key/value pairs you send in your post body.

Simple test route, so you see what you get back:

            'pattern' => 'rates',
            'method'  => 'POST',
            'action'  => function() {
                $data = kirby()->request()->data();

                F::write(kirby()->root('index') . '/data.txt', dump($data));
                return new Response('All good');

Ok, the data.txt contains the correct output:

    [eventName] => shippingrates.fetch
    [mode] => Test
    [createdOn] => 2020-07-29T09:07:26.2573841Z
    [content] => Array
            [token] =>...
            [isRecurringOrder] => 
            [parentToken] => 
            [parentInvoiceNumber] => 
            [subscriptionId] =>

How can I access the array keys/values?


$data = kirby()->request()->data();
$content = $data['content'];
$token     = $data['content']['token'];
// etc.

Ok, it was just a matter of removing the [‘body’] index from $data. I guess I messed up switching contexts between the mock data I used in Insomnia, and the real data. :man_facepalming: