无法向 API 平台提交数据

Can’t submit data to API Platform

我有一个控制器,通过它我可以将数据发送到 api 平台的本地环境(运行 很好)

    /**
     * Creates a new Location entity.
     *
     */
    public function createAction(Request $request)
    {
        $this->get("suvya_breadcrumbs.factory")->update('New Hub');

        $form = $this->createForm(HubType::class);
        $form->handleRequest($request);

        $new_url = $this->generateUrl('new_hub');
        $redirect_url = $this->generateUrl('home');

        if ($form->isSubmitted() && $form->isValid()) {

            $client = new Client([
                'base_uri' => 'http://127.0.0.1:8000',
            ]);

            $response = $client->request('POST', '/hubs', [
                'form_params'=> $form->getData(),
                'headers' => [
                    'Accept'     => 'application/ld+json',
                    'Content-Type'=> 'application/json',
                    'Authorization'=> 'eyJhbGciOiJSUzI1NiJ9.eyJyb2xlcyI6WyJST0xFX1VTRVIiXSwidXNlcm5hbWUiOiJhZG1pbiIsImlhdCI6MTUxNzg3NjE1NCwiZXhwIjoxNTE3ODc5NzU0fQ.gIG9lueJJZxzkOl8qblhHWwiJvW97m4gz1-1mYeM9SgMzMW35Wh6XamOOYiISDN99yJ6Ovo-wKk6whpE5UGMDVw_wGek003Dd6r-Y7Ql3kVLHksn2JFzhAN3GwlXFcOI4MIjmq5qBhkzv21pHymO0yn1SlzWBwb0O7WygywefMu5p09zGuvAiP9I2ShyQLZhjj8bB_odf3dI-Ql0ZbRmn_JDkDoPcm5U11i-3S1oMikBmFq0WtTcWo7vezt3QdA3bY4_bgaISINAiYRR-_cvjpBSqFSE6n1ZYtHvKFn-98wXXsBGxEAoZw6iQL4iRgOI8F_uaiCo0eRHC7q0_xQ_V_W0-5XDIQXWDwoiVaUXnjO6xo2Fldp7PLO1ueJz1e4wiOy2-TunZdc8UCtw2BdFIQtWatPLi_v_rsNvF2H-6hwa9UOKEi9Z4tH4KkuATbXAxxfkCbSOyY1SAWP0riooPQi_AI2J7L2Ly86eAuKo1Hix3EuEogo19GSyBz_cCWczyERQWM9gikuUs8E22SIAdxTl8ZLFaXgiZIibDvb8pqcN8izFjywWbF2CkyWC58WxrVd6Bfmfnm7k9T6oZqwIZ-TQR-SbRnUHN1hpWUjFCk-tHhgvh7osHXmxe3grzA8M3LPBpQGQiTeqBZFMjF4Tx8zW2tuiEn6TwhV14Lj24Vc'
                ]
            ]);

            die();
        }

        return $this->render('SuvyaFabricsCloudBundle:Common:basic.html.twig', array(
            'form' => $form->createView(),
            'page_title'=> 'Hub aanmaken',
            'action_path' => $new_url,
            'cancel_path'=> $redirect_url,
            'submit_button_title'  => 'Opslaan',
        ));
    }

但不幸的是,我在 api 环境的 dev.log 中遇到错误:

[2018-02-06 02:32:37] request.INFO: Matched route "api_hubs_post_collection". {"route":"api_hubs_post_collection","route_parameters":{"_controller":"api_platform.action.post_collection","_format":null,"_api_resource_class":"AppBundle\Entity\Hub","_api_collection_operation_name":"post","_route":"api_hubs_post_collection"},"request_uri":"http://127.0.0.1:8000/hubs","method":"POST"} []
[2018-02-06 02:32:37] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Serializer\Exception\UnexpectedValueException: "Syntax error" at /Users/myname/Sites/suvyalogistics-api/vendor/symfony/symfony/src/Symfony/Component/Serializer/Encoder/JsonDecode.php line 78 {"exception":"[object] (Symfony\Component\Serializer\Exception\UnexpectedValueException(code: 0): Syntax error at /Users/myname/Sites/suvyalogistics-api/vendor/symfony/symfony/src/Symfony/Component/Serializer/Encoder/JsonDecode.php:78)"} []

form->getData()的输出:

array (size=4)
  'postalCode' => string 'sdfdsf' (length=6)
  'streetName' => string 'sdfsdf' (length=6)
  'doorNumber' => string 'sdfsfd' (length=6)
  'city' => string 'sdfsfd' (length=6)

运行 具有相同值的 api 平台上的端点也正常。

你们中有人知道为什么会出错吗?

您正在向 API 平台 API 发送以 application/x-www-form-urlencoded(类似 HTML 形式)编码的有效负载,而根据 headers 你已经设置了,它不包括编码在 JSON.

中的数据

要对 JSON 中的表单数据进行编码,您可以使用以下 Guzzle 代码段:

$client = new Client([
    'base_uri' => 'http://127.0.0.1:8000',
]);

$response = $client->request('POST', '/hubs', [
    'json' => $form->getData(),
    'headers' => [
        'Accept' => 'application/ld+json',
        'Authorization'=> '...'
    ]
]);

或者,您可以 configure API Platform to accept form data,但我不建议这样做(最好只处理 JSON API-side)。