I’m trying to send a post request to my php endpoint from vue.js. This is the code :
$router->post('/add', function () {
$json = file_get_contents('php://input');
echo $json;
});
If I echo the json , I get an object as a response :
{
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": "{"SKU":"ABC123","name":"Product Name","price":19.99,"type":1,"size":2,"weight":1.5,"height":10,"width":5,"length":15}"
}
However, when I try to decode it like this :
$router->post('/add', function () {
$json = file_get_contents('php://input');
$data = json_decode($json,true);
echo $data;
});
I get this response : Array.
I can’t figure out why I’m not getting a decoded jsos, I’d appreciate any help, this is the post request from vue :
axios.post('http://127.0.0.1:8000/add', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
SKU: 'ABC123',
name: 'Product Name',
price: 19.99,
type: 1,
size: 2,
weight: 1.5,
height: 10,
width: 5,
length: 15
})
})
.then(res => console.log(res))
.catch(error => {
console.error('Error:', error);
});