3

Can you explain me why Axios request does not work in my Symfony application?

I use Axios request with React like this :

handleSubmit = () => {
    axios.put('/families/' + this.props.familyId + '/edit',{
        parents: "test"
    })
        .then(response => {
            alert('Family has been modified');
        })
};

My controller :

/**
 * Edit Family
 *
 * @Route("families/{id}/edit", name="family_edit")
 * @Method(methods={"PUT"})
 * @param Request $request
 * @param $id
 */
public function editFamilyAction(Request $request, $id)
{
    $parents = $request->get('parents');

    ...
}

But $parents equals null...

What is happening, do I need some configuration somewhere?

Help needed please!

Denis

  • If you look at the request in the Network tab of your Developer Tools, is it showing any errors? – Tholle Nov 16 '18 at 18:39
  • I have an error 500, because i do not return statement in my fonction. I do not return statement for now because my fonction is not finalised. Is it the problem? I debug it in PhpStorm. – Denis Ancelle Nov 16 '18 at 18:54
  • I'm not quite sure. You could return some mock data in the meantime to rule out that that is not the issue. – Tholle Nov 16 '18 at 19:05
  • Okay so I have tried and no error returned in Network tab... – Denis Ancelle Nov 16 '18 at 19:16
  • Possible duplicate of [Posting JSON objects to Symfony 2](https://stackoverflow.com/questions/9522029/posting-json-objects-to-symfony-2) – mblaettermann Nov 16 '18 at 20:14
  • AXIOS is sending the request as encoded json body, but you are accessing the (empty) form data headers. Look at https://stackoverflow.com/questions/9522029/posting-json-objects-to-symfony-2 – mblaettermann Nov 16 '18 at 20:15

2 Answers2

3

It's simple as that:

/**
 * @Route("/families/{id}/edit", name="family_edit", methods={"PUT"})
 * @param Request $request
 * @param $id
 */
public function editFamilyAction(Request $request, $id)
{

    $data = $request->getContent();
    $data = json_decode($data);
    $parents = $data->parents;

    // do your stuff
}

Note that if you are using Symfony 4, Method annotation has been deprecated, instead you should configure the Route annotation properly like I did in the code above.

iiirxs
  • 4,493
  • 2
  • 20
  • 35
0

Wow it seems that it is working ! thanks you very very much guy!!

Can you explain me what is my mistake please?

I have just copy/pasted team code without success. Let me explain the code :

React :

 handleSaveDiagnostic = () => {
    axios.put('/admin/api/diagnostic/update-diagnostic/'+ this.state.currentDiagnostic.id, {
        'newDiagnostic': this.state.currentDiagnostic
    })
        .then(response => {
            alert('Family has been modified');
            this.loadCounters();
        })
};

Controller :

* @Route("/update-diagnostic/{diagnostic}", name="update_diagnostic")
 * @Method(methods={"PUT"})
 *
 * @param \Symfony\Component\HttpFoundation\Request $request
 * @param \AppBundle\Entity\Diagnostic $diagnostic
 *
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function updateDiagnosticAction(Request $request, Diagnostic $diagnostic) {

    $newDiagnostic = $request->get('newDiagnostic'); (is working!)

    ...
}

What's the difference please, it is working for him but $request->getContent is needed for me??!!

  • This does not seem to be an answer. More like a second question? Big no no around here. – Cerad Nov 17 '18 at 01:32