Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access body from POST request (using 'fetch()') inside my Extbase Controller

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading