Laravel-json-api: Question: How to handle multipart/form-data

Created on 19 Apr 2019  路  6Comments  路  Source: cloudcreativity/laravel-json-api

I am trying to upload an image using the JSON API.
I already added the ContentNegotiator resource-specific to the images resource.
I followed the docs and added this code in order to accept multipart/form-data headers:

protected $decoding = [
        'multipart/form-data' => \App\JsonApi\MultipartDecoder::class,
        'multipart/form-data; boundary=*' => \App\JsonApi\MultipartDecoder::class,
    ];

At this point I am struggling to find or provide the MultipartDecoder class.
Can you explain to me, what I need to do next and how I can retrieve the uploaded file in the controller?

Thank's in advance!

docs

Most helpful comment

Have you also changed the create method in your validators class, as per the docs?

All 6 comments

Hi! Yes you'd have to write the MultipartDecoder class yourself. Sounds like I need to update the docs to make that a bit clearer and add an example class.

There's an example of what it might sort of be here in the test dummy app:
https://github.com/cloudcreativity/laravel-json-api/blob/develop/tests/dummy/app/JsonApi/FileDecoder.php

Thank you for updating the docs.
I also needed to override the data validation function since there was no body with the data object and type property in the request.
I switched to uploading the image as base64 encoded string because it鈥榮 much cleaner for me and allows me to pass json data along with the image.
It鈥榮 still good to know that multipart/form-data works, when I need to upload larger files.

Hello! I'm sorry for commenting on closed issues.

I looked on file handling on Avatar resource in the dummy test. It placed a create function on the Adapter, which i try on my case but it never got executed. Shouldn't it be placed on the AvatarController?

Below is the error message when send a form-data request

ERROR: Expecting a resource type. {"exception":"[object] (InvalidArgumentException(code: 0): Expecting a resource type. at ...\\vendor\\cloudcreativity\\laravel-json-api\\src\\Document\\ResourceObject.php:79)

Have you also changed the create method in your validators class, as per the docs?

Oh crap, silly me. That's make it work.
I'm still grasping the concept used in this package

Thank you so much @lindyhopchris

Ah no problem!

Was this page helpful?
0 / 5 - 0 ratings