API Post 至 Sonata Media Bundle Symfony 3

API Post to Sonata Media Bundle Symfony 3

我的目的是使用 Sonata Media Bundle 从 Ionic 应用程序(现在使用 PostMan 进行测试)将图片发送到我基于 Symfony 3 的数据库。

他们是 documentation 但它很短,我坚持使用 15.3。发送媒体文件过程。

我设法在 Bundle 中获得了后续流程并检查了数据。 c:\wamp64\www\bumblb___api\vendor\sonata-project\media-bundle\Controller\Api\MediaController.php > handleWriteMedium() 函数。

/**
 * Write a medium, this method is used by both POST and PUT action methods.
 *
 * @param Request                $request
 * @param MediaInterface         $media
 * @param MediaProviderInterface $provider
 *
 * @return View|FormInterface
 */
protected function handleWriteMedium(Request $request, MediaInterface $media, MediaProviderInterface $provider)
{
    $form = $this->formFactory->createNamed(null, 'sonata_media_api_form_media', $media, array(
        'provider_name' => $provider->getName(),
        'csrf_protection' => false,
    ));

    // return $media;
    // return $request->__toString();

    // THE FORM DOES NOT FILL WITH REQUEST
    $form->handleRequest($request);
    return $form->getData();
    // return $media;


    if ($form->isValid()) {

        $media = $form->getData();
        $this->mediaManager->save($media);

        $view = FOSRestView::create($media);

        // BC for FOSRestBundle < 2.0
        if (method_exists($view, 'setSerializationContext')) {
            $serializationContext = SerializationContext::create();
            $serializationContext->setGroups(array('sonata_api_read'));
            $serializationContext->enableMaxDepthChecks();
            $view->setSerializationContext($serializationContext);
        } else {
            $context = new Context();
            $context->setGroups(array('sonata_api_read'));
            $context->setMaxDepth(0);
            $view->setContext($context);
        }

        return $view;
    } else {
        // return "NOT VALID";
    }

    return $form;
}

这就是所谓的:

/**
 * Adds a medium of given provider
 * If you need to upload a file (depends on the provider) you will need to do so by sending content as a multipart/form-data HTTP Request
 * See documentation for more details.
 *
 * @ApiDoc(
 *  resource=true,
 *  input={"class"="sonata_media_api_form_media", "name"="", "groups"={"sonata_api_write"}},
 *  output={"class"="Sonata\MediaBundle\Model\Media", "groups"={"sonata_api_read"}},
 *  statusCodes={
 *      200="Returned when successful",
 *      400="Returned when an error has occurred while medium creation",
 *      404="Returned when unable to find medium"
 *  }
 * )
 *
 * @Route(requirements={"provider"="[A-Za-z0-9.]*"})
 *
 * @param string  $provider A media provider
 * @param Request $request  A Symfony request
 *
 * @return MediaInterface
 *
 * @throws NotFoundHttpException
 */
public function postProviderMediumAction($provider, Request $request)
{
    $medium = $this->mediaManager->create();
    $medium->setProviderName($provider);

    try {
        $mediaProvider = $this->mediaPool->getProvider($provider);
    } catch (\RuntimeException $ex) {
        throw new NotFoundHttpException($ex->getMessage(), $ex);
    } catch (\InvalidArgumentException $ex) {
        throw new NotFoundHttpException($ex->getMessage(), $ex);
    }

    return $this->handleWriteMedium($request, $medium, $mediaProvider);
}

我选择使用 JSON 和 POSTMAN 来制作它。表单数据似乎不起作用。

http://127.0.0.1:8000/api/providers/sonata.media.provider.image/media

{
"name": "nameex",
"description": "descriptionex",
"copyright": "copyrightex",
"authorName": "authorNameex",
"cdnIsFlushable": true,
"enabled": true,
"binaryContent": "data:image/jpeg;base64,test"
}

我写来测试的差异 return :

return $form->getData();

{
        "provider_metadata": [],
        "enabled": false,
        "provider_name": "sonata.media.provider.image"
    }

return $request->__toString();

"POST /api/providers/sonata.media.provider.image/media HTTP/1.1\r\nAccept:          */*\r\nAccept-Encoding: gzip, deflate, br\r\nAccept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4\r\nAuthorization:   Basic c3VwZXI6c3VwZXI=\r\nCache-Control:   no-cache\r\nConnection:      keep-alive\r\nContent-Length:  201\r\nContent-Type:    application/json\r\nHost:            127.0.0.1:8000\r\nOrigin:          chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop\r\nPhp-Auth-Pw:     super\r\nPhp-Auth-User:   super\r\nPostman-Token:   16103fb6-3847-efe8-9cc6-7aa2a48b4ff9\r\nUser-Agent:      Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36\r\nX-Php-Ob-Level:  1\r\n\r\n{\n\t\"name\": \"namee\",\n\t\"description\": \"description\",\n\t\"copyright\": \"copyright\",\n\t\"authorName\": \"authorName\",\n\t\"cdnIsFlushable\": true,\n\t\"enabled\": true,\n\t\"binaryContent\": \"data:image/jpeg;base64,patata\"\n}"

请求似乎没问题,但表格没有填写。我将该请求与另一种有效的形式 post 进行比较,它的结构相同。

表格永远无效,因为 "should not be blank".

我可能会找到以另一种方式测试表单数据的解决方案。尝试发送真实的 baseEncode 图像值。

我知道这是一个非常具体的案例,但如果有人在这种情况下或者能够提供我不同的观点,那就太好了。或者我将不得不放弃与管理员和接待人员完美合作的 Media Bundle :(

谢谢

解决方案只是在使用表单数据类型格式时不要覆盖 POSTMAN Headers Content-Type。

覆盖 POSTMAN Content-Type 使正在发送的 POST 为空..

在 Postman 中测试后,我使用 DATA_URL 图片格式使用 Ionic 和 MediaBundle API 发送它。 (使用 Blob 转换和 FormData javascript 系统)