I am making simple symfony api and react app. My react app sends data in json format to the symfony api. However I am unable to access the data that is being sent.
Here is the function in the component in my react app -
createProduct (event) {
event.preventDefault()
if(this.state.name.length === 0 ){
this.error = 'Name cannot be empty'
}else if(this.state.price === 0){
this.error = 'Price cannot be 0'
}else if(this.state.price < 0 ){
this.error = 'Price cannot be negative'
}else {
let data = []
data['name'] = this.state.name
data['price'] = this.state.price
ProductActions.addProduct(data)
}
}
and here is the actual addProduct
action -
addProduct (data) {
console.log('made it')
let request = {
method: 'POST',
url: 'http://127.0.0.1:8000/product/post',
data: JSON.stringify(data),
contentType: 'application/json'
}
$.ajax(request)
.done(data => this.addProductSuccess(data))
.fail(err => this.addProductFail(err))
}
And now the Symfony api - here is the action taking care of creating new product
/**
* Creates a new product entity.
* @Rest\Post("/post")
*/
function newAction(Request $request ) {
$body = $request->getContent();
$body = json_decode($body,true);
var_dump($body);
dump($body);
$logger = $this->get('logger');
$logger->error($request);
//$productPrice = $body['price'];
//$product = new Product($productName,$productPrice);
//$em = $this->getDoctrine()->getManager();
//$em->persist($product);
//$em->flush();
//$data['data'] = $product;
$data['body'] = $body;
return $data;
}
As you can see most of the code is in comments. The reason being it doesnot work. After this line of code everything breaks
$productPrice = $body['price'];
And here is the error I receive
Undefined index: price"
This code was working fine and I was able to get the data in the context of the request easily, but I decided to start using FOSBundle and now it doesn't work.
As you can see there is var_dump($request)
as well as dump($request)
in attempts to view the Request being sent.I have also as you can see tried returning the request to my react api to view it there but it doesnot work and this is the error I get -
Resources are not supported in serialized data
And if it matters - I have before that tried returning new View($data,{some response code})
however it gives this error -
Unknown key "request" for annotation "@FOS\RestBundle\Controller\Annotations\View
which I have no idea what it means. It says this for everything.If I send normal array it will say Unknown key '0'
and I have no annotations such as @View and to be honest I have no idea how to use them.
And after those attempts to view the request didn't work I used the $logger
. Here is what is actually being sent
POST /product/post HTTP/1.1 Accept: / Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8 Connection: keep-alive Content-Length: 2 Content-Type: application/json Host: 127.0.0.1:8000 Origin: http://localhost:3000 Referer: http://localhost:3000/ User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36 X-Php-Ob-Level: 1 [] [] []
I am unable to view the Request in any other manner.
I have seen tutorials that suggest having Symfony FormType to handle submits, but the thing is I have no front-end part and no forms being created in the first place to be submitted. This symfony API is only back-end I am not sure how to send the data from my react app to the symfony api if I was to use symfony forms.
Can someone please tell me how to get the data from the request and how to return it since return new View({any data}, {some response code})
gives Unknown key {whatever key I have sent}
?
[Solved (kind of)] - When creating the data to pass to the symfony api it turned out that for some reason I have written []
instead of {}
and this caused the error in not sending the right values.
However this keeps the other part of the question - how to use @View. Cause whenever I try to return new View({some data}, {some response code});
it throws an error.