skip to Main Content

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:

fetch("myDomain.de/my/route/Enhancer/", {
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({ cookieValue: cookie})
})

in the Controller

    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


  1. Since TYPO3v11 you can access $this->request for the (decorated) request. This way you can access the request body:

    $this->request->getBody()
    

    However, since this will only give you the body as raw stream, you can use this convenience wrapper:

    $this->request->getParsedBody()
    
    Login or Signup to reply.
  2. 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:

    public function syncAction()
    {
        $arguments = $this->request->getOriginalRequestMappingArguments();
        $body = $arguments['__body'];
        
        // $body variable now contains the JSON-encoded body data sent from the Service Worker
        
        // ... Process the data
        
        // Return a response if needed
        $response = new TYPO3CMSCoreHttpResponse();
        $response->getBody()->write('Data received successfully');
        return $response;
    }
    

    In your Service Worker, you can modify the fetch() call to include the body data as a separate route argument:

    fetch("myDomain.de/my/route/Enhancer/__body", {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({ cookieValue: cookie})
    });
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search