I have an application in TYPO3 CMS. It has an extension test_extension that has a controller and an action. This action should return some JSON.
class TestRequestController extends ActionController
{
public function testAction(): void
{
echo json_encode([
'test' => 123
]);
}
}
I want to be able to request this action via Postman. How can I do that? TYPO3 version - 8.7. Thanks in advance!
Creating Links
Usually extbase-extensions are created with the help of the extension
extension_builder. This extension creates by default templates and links to open list- and detail-view.It's possible to add additional actions and to create according links. Also the usage of the templates is not required and your way to return the result of the action without usage of a template is possible. The logic of the links is this, I break the parts down in single lines:
There are still more parameters commonly used like
idorsearch, but it's also possible to define individual parameters.In your case the listed parameters seem to be sufficient and the link would look like this now:
for the extension news this would look like this, this is with your parameter-values which are not available in news. This example shows only how the extension-related part is handled (tx_news_pi1):
idis for the page here and not a parameter for your extension, else id had to look like thistx_extension_plugin[id]=123. So all extension related parameters have the formtx_extension_plugin[parameter]=value. While it's possible to create those links with the API, it's easier to create them with the view helpers for the fluid templates. Note that sometimes an hash is added at the end, like this example:&cHash=1234567890.The
cHash-value you can't create without viewHelper or API, so the knowledge about the parameters and the other values is not enough to create the links.Calling the link
Most often links are directly called by the browser and visible in the URL-bar. But sometimes and in your case you might call the links by
AJAX, so that the json is loaded without being directly shown to the user.It's also possible to wrap the json in script-tags, so that it's every time loaded when the whole page is called, it's not dynamic then and without AJAX it can't adjust to some user-interaction without loading a whole page again.
AJAXresponses can be realized in many ways in TYPO3, the most easy one is to define a special page-type and a special page in the pagetree for it. On this page you add the plugin of your extension to return the json. This "Ajax-page" has to be configured to have the correct header for Json and must not return anything else but the JSON, so all HTML-Output has to be disabled.