Remote::request, invalid JSON-data

I would like to connect two Kirby instances. A hook in the sender instance sends data to the receiver instance. The receiver then updates the corresponding pages.

My problem is that I cannot decode the data in the receiver instance properly.

The sender uses a hook to send the data. Data gets encoded to JSON:

'hooks' => [
	'page.update:after' => function($newPage, $oldPage) {
		$request = Remote::request("https://receiver.test/update", [
			'method' => 'POST',
			'data' => Json::encode([
			   'page' => [
			      'id' => 'test'
			   ]
			]),
			'headers' => [
			   'Authorization: Basic ' . base64_encode($user . ':' . $pass),
			   'Content-Type: application/json'
			]
		]);
	}
]

The receiver instance uses a route to process the data:

'routes' => [
	[
	   'pattern' => 'test',
	   'method'  => 'POST',
	   'action'  => function () {
	      $data = kirby()->request()->data();
	      ...
	   }
	]
]

According to the docs, $request->data() should decode JSON and return an array. But the dump of $data looks like this:

Array
(
    [{"page":{"id":"test"}}] => 
)

Consequently, I can’t access $data['page'] (error: Undefined index: page).
Trying to Json::decode(kirby()->request()) results in the error: Invalid JSON data; please pass a string.

I would rather expect $data to look like this:

Array
(
    "page" => [
		"id" => "test"
	]
)

How can I properly decode the incoming data?

I tested your example with two local Starterkit instances, writing the result to file:

'action'  => function () {
          $data = kirby()->request()->data();
          F::write(kirby()->root('index') . '/test.txt', dump($data));
       }

And get the expected array in test.txt

<pre>Array
(
    [page] => Array
        (
            [id] => notes/exploring-the-universe
        )

)
</pre>

Tested with 3.5.7.1. Which Kirby version are you using?

Also Kirby version 3.5.7.1 on both ends.
F::write again gives me this:

<pre>Array
(
    [{"page":{"id":"test"}}] => 
)
</pre>

So obviously there’s an error somewhere else. I’ll break down the code even more and try to find it.

Edit: Just tested it with two blank local plainkits (Laravel Valet, PHP 7.4.23).
The result is the same: as soon as I use Json::encode($array) to encode the data on sender side, the result gets messed up on receiver side.

Sendig an array works fine, though.

:man_facepalming: Just a typo! A hyphen was missing in the headers array:
'Content Type: application/json'

But how it sneaked into my sample code above, I have no idea!