kais
kais

Reputation: 21

How to send json and file with Symfony Api Pltaform

I'm using Symfony 4 and Api Platform and I have an entities Book and Docuement. Adding books and adding files works fine but separately. Now I'd like to add them together and link Book To Document.

Entity Book:

* @ORM\Entity
*/
class Book
{

//***

/**
 * @var Document|null
 *
 * @ORM\ManyToOne(targetEntity="App\Entity\Document", cascade={"all"})
 * @ORM\JoinColumn(nullable=true)
 * @ApiProperty(iri="http://schema.org/image")
 * @Groups({"book:read", "document_read", "document_create", "book:write"})
 */
public $document;

//***
}

When I add a Document with form-data using postman it works and the file is saved but when I add a Book with form-data I get this error:

The content-type "multipart/form-data; boundary=--------------------------431943027292248567654772" is not supported. Supported MIME types are "application/ld+json", "application/json", "text/html".",

postman body

img

Upvotes: 1

Views: 1440

Answers (1)

LebzoDev
LebzoDev

Reputation: 26

As said above, you have to add "mime_types" section in your api_platform.yml file (Config->packages->api_platform.yml).

Like this:

api_platform:

mapping:
    paths: ['%kernel.project_dir%/src/Entity']
patch_formats:
    mime_types: ['multipart/form-data']
    json: ['application/merge-patch+json']

Upvotes: 0

Related Questions