简体   繁体   中英

Symfony FOSRestBundle POST request 500 error

I'm trying to create an entity using FOSRestBundle.

The URL:

POST http://localhost:8000/app_dev.php/api/links

The request payload:

{"link":{"id":"5","title":"foo","description":"foo","url":"foo","image":null,"issue":"1","creator":"1"}
}

Here is the controller (genrated):

/**
 * Create a Link entity.
 *
 * @View(statusCode=201, serializerEnableMaxDepthChecks=true)
 *
 * @param Request $request
 *
 * @return Response
 *
 */
public function postAction(Request $request)
{
    $entity = new Link();
    $form = $this->createForm(new LinkType(), $entity, array("method" => $request->getMethod()));
    $this->removeExtraFields($request, $form);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $entity;
    }

    return FOSView::create(array('errors' => $form->getErrors()), Codes::HTTP_INTERNAL_SERVER_ERROR);
}

I get this 500 error I get, which is really unhelpful:

{"formErrorIterators":{"errors":{"form":{"children":{"title":[],"type":[],"summary":[],"description":[],"url":[],"image":[],"date":{"children":{"date":{"children":{"year":[],"month":[],"day":[]}},"time":{"children":{"hour":[],"minute":[]}}}},"creator":[],"issue":[]}},"errors":[]}}}

I think that the $Request is not sent or received properly, because at some point I desactivated csrf protection, and Doctrine wanted to create an Entity with only null fields.

This is my config.yml:

fos_rest:
    routing_loader:
        default_format: json
    param_fetcher_listener: true
    body_listener:
        array_normalizer: fos_rest.normalizer.camel_keys
    body_converter:
        enabled: true
    format_listener:
        rules:
          - { priorities: ['json'], fallback_format: json, prefer_extension: false }
    view:
        view_response_listener: force

Any thoughts ? Thanks

Okay, turns out there was several errors :

First, the JSON payload was malformed :

{"title":"foo","summary":null,"description":"foo","url":"foo","image":null,"issue":"1","creator":"1"}

(I removed the name of the node)

Then, the Type descriptor was not correct :

        $builder
        ->add('title', 'text')
        ->add('type', 'text')
        ->add('summary', 'text')
        ->add('description', 'text')
        ->add('url', 'text')
        ->add('image', 'text')
        ->add('date', 'date')
        ->add('creator',  'entity', array(
              'class' => 'AppBundle:User')
             )
        ->add('issue',  'entity', array(
                'class' => 'AppBundle:Issue')
        )
    ;

I added the types and entities to make it okay.

I'd have been happier with more precise error codes though.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM