0

I have a symfony web site and I'm doing its api rest, and I'm about to upload an image for an entity and I'm doing the postman tests, but when I realize it the JSON response turns me back to null. Note that I use vichuploaderbundle to upload my image.

Controller

/**
 * @Route("/api/admin/assurance/logo/{id}")
 * @Rest\View()
 * @Method("PUT")
 */
public function putLogoEditAction(
    Request $request,
    Assurance $assurance,
    $formType = null,
    $logoId = null
){
    $em = $this->getDoctrine()->getManager();
    $logofile = $request->files->get('file');
    $logofile = $this->getLogoFile($logoId);
    $assurance->setLogo($logofile);

    $form = $this->createForm('Doctix\MedecinBundle\Form\LogoType', $logofile);
    $form->submit($request->request->all());

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

        return $logofile;
     }

     return $form;
} 

private function getLogoFile($logoId)
{
    if (null === $logoId) {
        return new Logo();
     }
     $logofile = $this->getDoctrine()->getRepository('DoctixMedecinBundle:Logo')->find($logoId);

     return $logofile;
}

Entity

/*
 * @Vich\UploadableField(mapping="media_image", fileNameProperty="logoName")
 * 
 * @var File
 */
private $logoFile;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 *
 * @var string
 */
private $logoName;

Form

 ->add('logoFile', VichImageType::class, [
     'required' => false,
     'allow_delete' => true,
     'download_label' => true,
     'download_uri' => true,
     'image_uri' => true,
     'imagine_pattern' => 'null',
 ]);

Test with Postman

I try to add a logo to an insurance, through the insurance id, and for that I add the next json, but it returns me a null with a code 200. This is a screenshot of my postman: test with postman

Get with Postman

Here you will find on this screenshot the result of the GET request that summarizes the contents of the insurance entity that has a name and a logo, and to which a doctor can affiliate

GET Request

Thanks

Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
Mohamed Sacko
  • 233
  • 1
  • 2
  • 20
  • As far as I see you're sending a raw JSON `PUT` request without an actual file but only with the file-name in it from postman. Why do you expect a picture to be uploaded if you only provide the information/string `logoFile.file.name`? – Nicolai Fröhlich Jul 18 '19 at 14:16
  • @NicolaiFröhlich, so what should I provide then to upload the image, the path? – Mohamed Sacko Jul 18 '19 at 16:32
  • You'd provide the binary image-file but this not easily possible with a JSON API. You'd have to i.e. base64 encode the image-file on the client-side and then decode it on the server-side before processing it further. This "problem" can also be read about in issue [#246](https://github.com/json-api/json-api/issues/246) of the json-api specification. The "solution" is obviously to step back from JSON for file-uploads and submit your data in a normal form with enctype `multipart/form-data`! – Nicolai Fröhlich Jul 18 '19 at 16:49
  • You can read more about the topic in [this question](https://stackoverflow.com/questions/17129874/uploading-image-file-through-api-using-symfony2-fosrestbundle/23825541#23825541) and it's answers. I strongly recommend you to use a normal form upload for this API endpoint instead of JSON with i.e. the decoding/encoding solution i mentioned before. – Nicolai Fröhlich Jul 18 '19 at 16:57

0 Answers0