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
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
-
You can access the symfony http request from the RestRequest (
\Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface::getHttpRequest
)
this already has a property calledfiles
which 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.0 -
For sending the file via JS (I assume it's for a JS application): https://stackoverflow.com/a/28193031
0 -
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?0 -
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
0 -
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
0 -
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.
0 -
Technical you can. You can also still return json in the response.
0 -
but the request also comes in as json
0 -
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
).0 -
the json does not include file upload yet
0 -
I am aware of the php limitation, have done file uploads before, but not with rest...
0 -
question is: how does the base64 string to be put into the json with what validation config
0 -
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
0 -
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?
0 -
need to check, full morning of meetings, so didn't have the chance to check yet
0 -
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.
0
Categories
- All Categories
- 42 Getting Started & Guidelines
- 7 Getting Started in the Community
- 8 Additional Resources
- 7 Community Ideas and Feedback
- 76 Spryker News
- 929 Developer Corner
- 787 Spryker Development
- 89 Spryker Dev Environment
- 362 Spryker Releases
- 3 Oryx frontend framework
- 35 Propel ORM
- 68 Community Projects
- 3 Community Ideation Board
- 30 Hackathon
- 3 PHP Bridge
- 6 Gacela Project
- 26 Job Opportunities
- 3.2K π Slack Archives
- 116 Academy
- 5 Business Users
- 370 Docker
- 551 Slack General
- 2K Help
- 75 Knowledge Sharing
- 6 Random Stuff
- 4 Code Testing
- 32 Product & Business Questions
- 70 Spryker Safari Questions
- 50 Random