2

I've been creating a lot of webservices API endpoints in Joomla 4 lately and wanted to ask: "What's the simplest, fastest, most practical way to get the entire POST object when a controller method gets the incoming request for Joomla 3 & 4. Assume JSON (no HTML forms or XML) and no Joomla filtering".

I've tried to directly decode the POST superglobal i.e.
$req = json_decode( $_POST ) (generally bad practice to bypass J! entirely).

I've also tried the same with $_REQUEST to no avail. (again bad practice).

Here is the code I've come up with after crawling through the core classes:

$req = json_decode( $this->input->json->getRaw() );

That will take the raw json string and produce an isomorphic PHP object easily accessible via -> e.g.
$req->action, or $req->data->id

If you want an associative array (hashmap) as the output, then try this:

$req = json_decode( $this->input->json->getRaw(), true);

The above code will produce a similar isomorph of the JSON object, as an associative array. e.g.
$req["action"], or $req["data"]["id"]

Of course, being a hashmap, the latter option allows you to substitute keys with variables i.e.
$req["data"]["$key"]

I'm asking this (and asking for other working techniques of accessing the POST object in J! 3 & 4 (leaving 1.5 & 2.5 behind)) as the following references and SO Q/A pages were unhelpful (their solutions didn't yield access to the POST object at all).

Deprecated Joomla docs: https://docs.joomla.org/Retrieving_request_data_using_JInput

SO Q/A with answers mostly addressing EOL J! versions: What's the proper way to get the entire $_POST?

Does anyone know of a working technique for J! 3 & 4 that returns the full JSON POST object?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.