Symfony 4 Fos Rest : Error 415 Unsupported Media Type

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 AppController;

use AppEntityEvent;
use FOSRestBundleControllerAbstractFOSRestController;
use FOSRestBundleViewView;
use SymfonyComponentHttpFoundationResponse;
use FOSRestBundleControllerAnnotations as Rest;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
use SymfonyComponentValidatorConstraintViolationListInterface;

/**
 * Event controller.
 * @Route("/api", name="api_")
 */
class EventController extends AbstractFOSRestController
{
    /**
     * Create New Event
     * @RestPost("/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 AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraintsDateTime;
use SymfonyComponentValidatorConstraints as Assert;

/**
 * @ORMEntity
 * @ORMTable(name="event")
 */
class Event
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORMManyToOne(targetEntity="AppEntityUser")
     *
     */
    protected $organizer;

    /**
     * @ORMColumn(type="text")
     * @AssertNotBlank
     */
    protected $title;

    /**
     * @ORMColumn(type="text")
     */
    protected $description;

    /**
     * @ORMColumn(type="text")
     */
    protected $address;

    /**
     * @ORMColumn(type="text")
     */
    protected $city;

    /**
     * @ORMColumn(type="text")
     */
    protected $postCode;

    /**
     * @ORMColumn(type="text")
     */
    protected $coverPicture;

    /**
     * @ORMColumn(type="float")
     */
    protected $price;

    /**
     * @ORMColumn(type="integer")
     */
    protected $maxAttendees;

    /**
     * @ORMColumn(type="datetime")
     */
    protected $dateFrom;

    /**
     * @ORMColumn(type="datetime")
     */
    protected $dateTo;

    /**
     * @ORMColumn(type="integer")
     */
    protected $status;

    /**
     * @ORMColumn(type="boolean")
     */
    protected $featured;

    /**
     * List of attendees
     * @ORMManyToMany(targetEntity="User")
     * @ORMJoinTable(name="event_atendees",
     *      joinColumns={@ORMJoinColumn(name="event_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(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 ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *