Reputation: 141
I created a REST Api. From the front i send post values with axios. (i use ReactJS). In Back with Symfony, in my Controller i want to get post values. How can i do ? I did it:
from the front:
const data = new FormData();
let postData = {
source: 'lcl',
userLastname: lastname,
userFirstname: firstname,
text: message,
}
data.append('data', postData);
Axios.post('http://127.0.0.1:8000/send', data)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error)
});
And from my back in Controller i try this:
$data = $request->request->get('data');
the value return [object Object]... How can i get the value (source, userLastname etc.).
Thank you for help.
Upvotes: 1
Views: 4639
Reputation: 2290
you should decode your data:
$data = $request->request->get('data');
if (!empty($data)) {
$array = json_decode($data, true); // 2nd param to get ass array
$userLastname = $array['userLastname']; // etc...
}
Now $array
will be an array full of your JSON data. Remove the true parameter value in the json_decode() call to get a stdClass object.
Upvotes: 2