func...">

JSON PUT request using Slim framework

I am trying to make a PUT request for Slim using the following code:

<script type = "text/javascript"> function submitform() { var url = '/users/' + $('#user_id').val(); $('#myform').attr('action', url); var data = JSON.stringify({"value": $('#user_data').val()}); $('<input type="hidden" name="json"/>').val(data).appendTo('#myform'); $("#myform").submit(); } </script> <form id = "myform" method="post"> id: <input type = "text" id = "user_id"> data: <input type = "text" name = "value" id = "user_data"> <input type="hidden" name="_METHOD" value="PUT"/> <input type = "button" value = "submit" onClick='submitform()'> </form> 

My index.php file contains:

 $app->put('/users/:id', 'update'); function update($id) { $jsonmessage = \Slim\Slim::getInstance()->request(); $message = json_decode($jsonmessage->getBody()); // what do I put here ???? } 

What should I put in place of ???? to retrieve a parameter value. I see this if I use $ _POST ['json'], but I don't think it is REST compatible. Everything that I tried, for example $ message-> value, does not work (returns nothing).

Thanks.

UPDATE:

Someone from the Slim forum provided the following answer:

 $json = $jsonmessage->put('json'); $message = json_decode($json); 
+4
source share
3 answers

If you want to capture the JSON put request parameter, then Slim has a simple solution.

 //$app->request()->params('parameterKey'); $params = $app->request()->params('value'); 

I think I have your question and let me know if it solves.

0
source

$ Message-> value

json_decode will create a standard php object for you. You can also try var_dump ($ message) to see the structure of the object for debugging.

0
source

I use the code below to pull out the request body

 $request = Slim::getInstance()->request(); $user = json_decode($request->getBody()); 
0
source

Source: https://habr.com/ru/post/1486521/


All Articles