Impossible to create a new event, I always get 415 error I test with postman, if I test the same endpoint with ParamConverter annotation, I can reach the function. So the error should come from configuration
Here is my controller
<?php
namespace App\Controller;
use App\Entity\Event;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\View\View;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
* Event controller.
* @Route("/api", name="api_")
*/
class EventController extends AbstractFOSRestController
{
/**
* Create New Event
* @Rest\Post("/event/create")
* @param Event $event
* @param ConstraintViolationListInterface $violations
* @ParamConverter("event", converter="fos_rest.request_body")
* @throws
* @return View
*/
public function createAction(Event $event, ConstraintViolationListInterface $violations)
{
if (count($violations)) {
return View::create($violations, Response::HTTP_BAD_REQUEST);
}
$em = $this->getDoctrine()->getManager();
$em->persist($event);
$em->flush();
return View::create($event, Response::HTTP_OK);
}
}
This is Fost Rest configuration, I checked the documentation and it should be ok
# Read the documentation: https://symfony.com/doc/master/bundles/FOSRestBundle/index.html
fos_rest:
routing_loader:
default_format: json
include_format: false
body_listener: true
format_listener:
rules:
- { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
param_fetcher_listener: true
access_denied_listener:
json: true
view:
view_response_listener: 'force'
formats:
json: true
body_converter:
enabled: true
validate: true
validation_errors_argument: violations
And to finish, event entity
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="event")
*/
class Event
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*
*/
protected $organizer;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank
*/
protected $title;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\Column(type="text")
*/
protected $address;
/**
* @ORM\Column(type="text")
*/
protected $city;
/**
* @ORM\Column(type="text")
*/
protected $postCode;
/**
* @ORM\Column(type="text")
*/
protected $coverPicture;
/**
* @ORM\Column(type="float")
*/
protected $price;
/**
* @ORM\Column(type="integer")
*/
protected $maxAttendees;
/**
* @ORM\Column(type="datetime")
*/
protected $dateFrom;
/**
* @ORM\Column(type="datetime")
*/
protected $dateTo;
/**
* @ORM\Column(type="integer")
*/
protected $status;
/**
* @ORM\Column(type="boolean")
*/
protected $featured;
/**
* List of attendees
* @ORM\ManyToMany(targetEntity="User")
* @ORM\JoinTable(name="event_atendees",
* joinColumns={@ORM\JoinColumn(name="event_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="attendee_id", referencedColumnName="id")}
* )
*/
protected $attendees;
public function __construct()
{
$this->attendees = new ArrayCollection();
}
/**
* @return int
*/
public function getId() : int
{
return $this->id;
}
/**
* @param int $id
* @return Event
*/
public function setId(int $id)
{
$this->id = $id;
return $this;
}
/**
* @return User
*/
public function getOrganizer() : User
{
return $this->organizer;
}
/**
* @param User $organizer
* @return Event
*/
public function setOrganizer(User $organizer)
{
$this->organizer = $organizer;
return $this;
}
/**
* @return string
*/
public function getTitle() : string
{
return $this->title;
}
/**
* @param string $title
* @return Event
*/
public function setTitle(string $title)
{
$this->title = $title;
return $this;
}
/**
* @return string
*/
public function getDescription() : string
{
return $this->description;
}
/**
* @param string $description
* @return Event
*/
public function setDescription(string $description)
{
$this->description = $description;
return $this;
}
/**
* @return string
*/
public function getAddress() : string
{
return $this->address;
}
/**
* @param string $address
* @return Event
*/
public function setAddress(string $address)
{
$this->address = $address;
return $this;
}
/**
* @return string
*/
public function getCity() : string
{
return $this->city;
}
/**
* @param string $city
* @return Event
*/
public function setCity(string $city)
{
$this->city = $city;
return $this;
}
/**
* @return string
*/
public function getPostCode() : string
{
return $this->postCode;
}
/**
* @param string $postCode
* @return Event
*/
public function setPostCode(string $postCode)
{
$this->postCode = $postCode;
return $this;
}
/**
* @return string
*/
public function getCoverPicture() : string
{
return $this->coverPicture;
}
/**
* @param string $coverPicture
* @return Event
*/
public function setCoverPicture(string $coverPicture)
{
$this->coverPicture = $coverPicture;
return $this;
}
/**
* @return float
*/
public function getPrice() : float
{
return $this->price;
}
/**
* @param float $price
* @return Event
*/
public function setPrice(float $price)
{
$this->price = $price;
return $this;
}
/**
* @return int
*/
public function getMaxAttendees() : int
{
return $this->maxAttendees;
}
/**
* @param int $maxAttendees
* @return Event
*/
public function setMaxAttendees(int $maxAttendees)
{
$this->maxAttendees = $maxAttendees;
return $this;
}
/**
* @return DateTime
*/
public function getDateFrom() : DateTime
{
return $this->dateFrom;
}
/**
* @param DateTime $dateFrom
* @return Event
*/
public function setDateFrom(DateTime $dateFrom)
{
$this->dateFrom = $dateFrom;
return $this;
}
/**
* @return DateTime
*/
public function getDateTo() : DateTime
{
return $this->dateTo;
}
/**
* @param DateTime $dateTo
* @return Event
*/
public function setDateTo(DateTime $dateTo)
{
$this->dateTo = $dateTo;
return $this;
}
/**
* @return int
*/
public function getStatus() : int
{
return $this->status;
}
/**
* @param int $status
* @return Event
*/
public function setStatus(int $status)
{
$this->status = $status;
return $this;
}
/**
* @return bool
*/
public function getFeatured() : bool
{
return $this->featured;
}
/**
* @param bool $featured
* @return Event
*/
public function setFeatured(bool $featured)
{
$this->featured = $featured;
return $this;
}
/**
* @return User[]
*/
public function getAttendees() : ArrayCollection
{
return $this->attendees;
}
/**
* @param User[] $attendees
* @return Event
*/
public function setAttendees($attendees)
{
$this->attendees = $attendees;
return $this;
}
/**
* @param User $attendee
*/
public function addAttendee(User $attendee) {
$this->attendees->add($attendee);
}
/**
* @param User $attendee
*/
public function removeAttendee(User $attendee) {
$this->attendees->removeElement($attendee);
}
}
Is somebody have an idea ?