I am trying to make a PUT request to 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 the ???? to retrieve the value parameter. I can see it if I use $_POST['json'] but I don't think this is REST compliant. Everything else I've tried, such as $message->value, doesn't work (returns nothing).
Thanks.
UPDATE:
Somebody from the Slim forum provided the following answer:
$json = $jsonmessage->put('json');
$message = json_decode($json);
If you want to capture the parameter of your JSON
put request, then there is a simple solution in Slim.
//$app->request()->params('parameterKey');
$params = $app->request()->params('value');
I think I got your question and let me know if it solves.
$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.
I use the below code to pull the request body
$request = Slim::getInstance()->request();
$user = json_decode($request->getBody());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.