API Post to Sonata Media Bundle Symfony 3

Paul Leclerc

My purpose is to send a picture from an Ionic App (For now testing with PostMan) to my Symfony 3 based database using Sonata Media Bundle.

Their is a documentation but it's quite short and I'm stuck to the 15.3. SENDING A MEDIA FILE process.

I managed to get the follow process in the Bundle and inspect data. c:\wamp64\www\bumblb___api\vendor\sonata-project\media-bundle\Controller\Api\MediaController.php > handleWriteMedium() function.

/**
 * 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;
}

Which is called be this one :

/**
 * 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);
}

I choose to make it using JSON with POSTMAN. Form-data does not seems to work.

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"
}

the differents return that I write to test :

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}"

The request seems to be OK but the form is not being filled with it. I compare the request with another form post which is working, it's the same structure.

The form is never valid, because vield "should not be blank".

I will may find a solution testing form-data in another way.. Trying to send a real baseEncode image value..

I know it's a very specific case but if someone was in this case or was able to offer my a different point of view would be wonderfull. Or I will have to give up on Media Bundle which is perfectly working with the admin and reception just because of that :(

Thank you

Paul Leclerc

The solution was just to do not override POSTMAN Headers Content-Type when using form-data type format.

Overriding POSTMAN Content-Type makes the POST being send empty..

After my tests in Postman, I used DATA_URL picture format to send it using Ionic and the MediaBundle API. (with Blob conversion and FormData javascript system)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony 3 - Sonata Aplication Media Bundle error

From Dev

Sonata User Bundle + Symfony 3.x

From Dev

Sonata Media Bundle Override

From Dev

error in sonata media bundle

From Dev

Symfony Sonata Media Bundle add images/videos to a user

From Dev

How to override default upload path of sonata media bundle in symfony?

From Dev

How to override default upload path of sonata media bundle in symfony?

From Dev

Symfony 2 Sonata Media bundle: saving media file image without sonata admin

From Dev

sonata admin bundle symfony

From Dev

Sonata Media Bundle + AWS S3 - specify subdirectory?

From Dev

Sonata Media Bundle Installation error

From Dev

Sonata Media Bundle with Sonata Admin Bundle 3.0 (or 2.4)

From Dev

Issue Configuring Sonata Media Bundle with MongoDB

From Dev

Issue Configuring Sonata Media Bundle with MongoDB

From Dev

Batch actions with Sonata Admin Bundle on Symfony

From Dev

Sonata admin bundle , Symfony2

From Dev

Image Upload Issue with Symfony3, Doctrine 2 and Sonata Admin Bundle

From Dev

How to validation to sonata media type in symfony 2

From Dev

Display the Sonata Admin Bundle inside a view of a Symfony Bundle

From Dev

Sonata admin bundle preview image from some entity in list mapper without sonata media bundle

From Dev

How to get Sonata Media Bundle to generate pictures in separate folders

From Dev

How to get Sonata Media Bundle to generate pictures in separate folders

From Dev

Sonata Media Bundle - How to extend FormatThumbnail.php

From Dev

Lexik Bundle Symfony 3

From Dev

how to override a css of sonata admin bundle in symfony2

From Dev

The show action of Symfony 2 Sonata Admin Bundle is not working

From Dev

Symfony 2.3.6 and Sonata Admin Bundle : Empty dashboard and no errors

From Dev

Symfony2 and Sonata Admin Bundle - filter timestamp displayed as date

From Dev

Symfony2 and Sonata Admin Bundle - filter timestamp displayed as date

Related Related

  1. 1

    Symfony 3 - Sonata Aplication Media Bundle error

  2. 2

    Sonata User Bundle + Symfony 3.x

  3. 3

    Sonata Media Bundle Override

  4. 4

    error in sonata media bundle

  5. 5

    Symfony Sonata Media Bundle add images/videos to a user

  6. 6

    How to override default upload path of sonata media bundle in symfony?

  7. 7

    How to override default upload path of sonata media bundle in symfony?

  8. 8

    Symfony 2 Sonata Media bundle: saving media file image without sonata admin

  9. 9

    sonata admin bundle symfony

  10. 10

    Sonata Media Bundle + AWS S3 - specify subdirectory?

  11. 11

    Sonata Media Bundle Installation error

  12. 12

    Sonata Media Bundle with Sonata Admin Bundle 3.0 (or 2.4)

  13. 13

    Issue Configuring Sonata Media Bundle with MongoDB

  14. 14

    Issue Configuring Sonata Media Bundle with MongoDB

  15. 15

    Batch actions with Sonata Admin Bundle on Symfony

  16. 16

    Sonata admin bundle , Symfony2

  17. 17

    Image Upload Issue with Symfony3, Doctrine 2 and Sonata Admin Bundle

  18. 18

    How to validation to sonata media type in symfony 2

  19. 19

    Display the Sonata Admin Bundle inside a view of a Symfony Bundle

  20. 20

    Sonata admin bundle preview image from some entity in list mapper without sonata media bundle

  21. 21

    How to get Sonata Media Bundle to generate pictures in separate folders

  22. 22

    How to get Sonata Media Bundle to generate pictures in separate folders

  23. 23

    Sonata Media Bundle - How to extend FormatThumbnail.php

  24. 24

    Lexik Bundle Symfony 3

  25. 25

    how to override a css of sonata admin bundle in symfony2

  26. 26

    The show action of Symfony 2 Sonata Admin Bundle is not working

  27. 27

    Symfony 2.3.6 and Sonata Admin Bundle : Empty dashboard and no errors

  28. 28

    Symfony2 and Sonata Admin Bundle - filter timestamp displayed as date

  29. 29

    Symfony2 and Sonata Admin Bundle - filter timestamp displayed as date

HotTag

Archive