Hi there!
I’m having an issue regarding a custom route that I registered in a plugin that has me scratching my head for some days now. The route is supposed to accept a POST request containing some JSON that I will use to dynamically create pages. While the route is registered successfully and I can reach it and return content, I’m having trouble reading the JSON that I send to it.
I have been testing the route from JS as well as a Browser extension API tester (Talend) and found that as soon as I send the request with no specific header or a Content-Type: application/x-www-form-urlencoded
header, I can access the data using kirbys $request->body()
and get()
functions. As soon as I send the request with the appropriate Content-Type: application/json
header though, it stops working and the request body is completely empty. I’m not sure what’s causing this or if I misunderstand some fundamental thing. Does anyone see an issue with the route itself?
<?php
return Array(
'pattern' => '/(:any)/(:any)/add_comment.json', // pattern is topicId, postId
'method' => 'POST',
'action' => function($topicId, $postId) use ($kirby) {
$request = $kirby->request();
// all of these are empty as soon as request header is `Content-Type: application/json`
$data = $request->body()->toArray();
$data = $request->body();
$data = get();
return json_encode($data); // for now just returning the data to see if it can be read
}
);
As always, any help or clarification is much appreciated. Thanks!