简体   繁体   English

Symfony FOSRestBundle POST请求500错误

[英]Symfony FOSRestBundle POST request 500 error

I'm trying to create an entity using FOSRestBundle. 我正在尝试使用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: 我得到这个500错误,这确实没有帮助:

{"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. 我认为$ Request的发送或接收不正确,因为在某些时候我取消了csrf保护,并且Doctrine想创建一个只有空字段的Entity。

This is my config.yml: 这是我的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 : 首先,JSON有效负载格式错误:

{"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. 我本来会更高兴使用更精确的错误代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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