What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Hi, does anybody of you have an idea how to upload a file through glue api? There is a Symfony valid

U01LLUGR1F0
U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet
edited September 2021 in Help

Hi,
does anybody of you have an idea how to upload a file through glue api?
There is a Symfony validator for file (with mime types) and for image available, but simply putting the base64 encoded string into the json request won't help.
Use case: I would like to add the possibility to upload a file in the contact form and that goes through a glue api endpoint.

And if not possible via JSON, can Glue also handle multipart/form-data? πŸ€”

Comments

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    You can access the symfony http request from the RestRequest (\Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface::getHttpRequest)
    this already has a property called fileswhich holds all uploaded files within the request with a temporary path. So the only thing you will need to do is to move it to a permanent storage.

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    For sending the file via JS (I assume it's for a JS application): https://stackoverflow.com/a/28193031

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    well our shop is headless, I only provide the api, any frontend should be able to work with that, that's the point
    so the glue endpoint is rest so json format
    you are proposing to switch it to multipart?

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    gotta look at that function in detail, but I also need to be able to configure the field in the validation yaml for the endpoint

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    glue endpoint is rest so json format

    Yes, but in this case I would rather comply with the http standard on how to send and receive files instead of converting them to base64 or something.

    Regarding validation you might be able to use the standard symfony file validator with that approach

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    Yep, but I can't just change the current endpoint to suddenly not be json anymore, so that's why am looking for a compatible solution.

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    Technical you can. You can also still return json in the response.

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    but the request also comes in as json

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    And the json includes the file as base64? If so you can just decode it and write it to the temp storage on the server, validate if it is a file with the right mime-type and upload it to the storage where it should be stored permanently.
    Be careful about the file size, php has a configurable limit for the body size of a POST (post_max_size).

    If you want to convert the given json/base64 decoded file before it reaches the controller, for example to make use of the the standard glue request validation you should have a look at \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\FormatRequestPluginInterface or if this is a functionality you only want for one controller you can implement \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\FormattedControllerBeforeActionPluginInterface instead.

    Is that what you are looking for? If not please clarify what you are searching for, as both questions you asked were answered (how to implement an upload -> use multipart/form-data. Can Glue handle mutilpart/form-data -> yes, through RestRequestInterface::getHttpRequest()->files).

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    the json does not include file upload yet

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    I am aware of the php limitation, have done file uploads before, but not with rest...

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    question is: how does the base64 string to be put into the json with what validation config

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    because as I said initially, putting the string into the json body and configuring file or image in the validation yaml doesn't cut it

  • Alberto Reyer
    Alberto Reyer Posts: 690 πŸͺ - Explorer

    If you want to convert the given json/base64 decoded file before it reaches the controller, for example to make use of the the standard glue request validation you should have a look atΒ \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\FormatRequestPluginInterface

    Does this answer it?

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    need to check, full morning of meetings, so didn't have the chance to check yet

  • U01LLUGR1F0
    U01LLUGR1F0 Posts: 60 πŸ§‘πŸ»β€πŸš€ - Cadet

    sorry for the late reply
    I tried what you proposed.
    When I posted as multipart, it just ran forever without reaching the debugger.

    \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\FormattedControllerBeforeActionPluginInterface is not existing in our project (maybe some older version or feature missing)
    for \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\FormatRequestPluginInterface it would be helpful if there was any implementation already in the code or more documentation about this, meaning what is supposed to be taken out and put in. Didn't really get anywhere there, because where to put the file in the given object, also I need to extend the transfer for the endpoint and I cannot specifiy a file, only string... so how to use the validation via yaml remains a mystery.

    Ultimately it was decided that other topics are more pressing, so I didn't continue working on it.