I'm trying to send an image to my API with React using a formData and Axios but my request in my custom controller shows null. When I use PostMan my api accepts the image fine.
In the headers of my axios request I have to add a boundary when without, it tells me a "missing boundary" error, however in the doc I can't find any info about that...
This is my request with Axios
const bodyFormData = new FormData()
bodyFormData.append('name',info.name)
bodyFormData.append('description',info.description)
bodyFormData.append('price',info.price)
bodyFormData.append('file', info.image,"image.jpg")
await Axios({
method: "post",
url: "http://127.0.0.1:8000/api/products/upload",
data: bodyFormData,
headers: { 'Content-Type': `multipart/form-data; boundary=${bodyFormData._boundary}` },
}).then(response=>console.log(response.data));
and my custom controller (works with PostMan)
<?php
namespace App\Controller;
use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class UploadProductController extends AbstractController
{
private $manager;
public function __construct(EntityManagerInterface $manager){
$this->manager = $manager;
}
public function __invoke(Request $request)
{
$uploadedFile = $request->files->get('file');
if (!$uploadedFile) {
throw new BadRequestHttpException('"file" is required');
}
dd($uploadedFile);
}
}
the answer is always "file is required" with React but with PostMan I have a file...