I’m sending a POST request via fetch() from my JavaScript Service-Worker to my TYPO3 Extbase controller action.
The controller then goes on to process the data, but I can’t find a way to get the body data.
I’m calling the Action through an Extbase RouteEnhancer, but I think this shouldn’t affect my Problem.
in the Service Worker: javascript
fetch("myDomain.de/my/route/Enhancer/", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ cookieValue: cookie})
})
public function syncAction()
{
?? Get Body from fetch() here??
return file_get_contents('php://input'); <-- is empty
}
I want to retrieve the body data from fetch.
I’ve tried:
$request = $GLOBALS['TYPO3_REQUEST'];
$data = $request->getParsedBody();
returns NULL
$data = $this->request->getParsedBody();
returns NULL
file_get_contents('php://input');
is empty
Am I missing something?
Is this the right way to approach or may there be a better solution to send the data?
2
Answers
Since TYPO3v11 you can access
$this->request
for the (decorated) request. This way you can access the request body:However, since this will only give you the body as raw stream, you can use this convenience wrapper:
In TYPO3 Extbase, to retrieve the body data from a POST request sent via fetch() in your Service Worker, you can access the request object and retrieve the body content using the getOriginalRequestMappingArguments() method. Here’s an example of how you can modify your controller action to retrieve the body data:
In your Service Worker, you can modify the fetch() call to include the body data as a separate route argument:
By appending /__body to your route, you ensure that the body data is passed as a separate argument that can be accessed using the getOriginalRequestMappingArguments() method as shown in the controller action above.
Please note that modifying the route with /__body is just a convention, and you can choose any other route segment that suits your needs. The important part is to retrieve the value using getOriginalRequestMappingArguments() and the corresponding route segment you used.
This approach should allow you to retrieve the body data sent from the Service Worker in your TYPO3 Extbase controller action.