Implementing an API receiving multipart/form-data

0
Hellomy objective is to implement an API in my Mendix Application that receives an multipart/form-data content type, with two parts : - file : the binary data of the file- metadata : a json for the file metadata (name, category, etc...)As a result my application could answer to this curl call : curl -F metada='{"category":"confidential"}' -F file=@myfile.pdf http://myapp.com/api/documents I tried to implement my API with a microflow that receieves HttpRequest as a paramater, my the HttpRequest/content attribute is empty, and I assume it won't support my form-data anyway because it's a String type value.I tried to implement a custom java function, and try to get the HttpServletRequest from the system.proxies.HttpRequest without any success.I noticed that my form-data is well transmitted to my Mendix app, because I saw it, with the two parts well filled, in an error message logged by the app.Has anyone implemented such feature?
asked
3 answers
1

Hi Guillaume Delafosse

I have done this before but is TDS because you can do this with a custom java action and a handler concept , The way of your java action failed because You must use Core.addRequestHandler() with a custom RequestHandler class to get access to the raw HttpServletRequestthat is the only reliable entry point for multipart parsing in Mendix.


if wanted I can give you step by step to implement the concept. I hope this helps

answered
0

Hi,


Mendix does not natively support parsing multipart/form-data (mixed file + JSON) directly via standard Published REST services or HttpRequest objects. That’s why HttpRequest/content appears empty or unusable for this case.

To implement this correctly, you need to handle the request at a lower level.


1. Use a Custom Java Action

Handle the multipart request using HttpServletRequest:

  • Create a Java action with HttpRequest as input
  • Extract the raw servlet request:

HttpServletRequest request = (HttpServletRequest) getContext().getHttpServletRequest();

  • Use a library like Apache Commons FileUpload or Servlet 3.0 APIs:

Collection<Part> parts = request.getParts();

Then:

  • Read file part → store in System.FileDocument
  • Read metadata part → parse JSON → map to entity

2. Store file in Mendix

Create a FileDocument and store stream:


Core.storeFileDocumentContent(context, fileDoc, inputStream);

3. Parse metadata JSON

  • Convert metadata part to String
  • Use Core.deserializeJsonToObject() or import mapping

  • Default Mendix REST handling does not support multipart parsing
  • HttpRequest.getContent() only works for raw bodies (JSON/XML), not multipart
  • You must handle file + metadata separately from request parts

Alternative (simpler design)

If possible, split the API:

  1. Upload file (multipart → FileDocument)
  2. Send metadata (JSON → REST endpoint)

This avoids custom Java and is easier to maintain.


To support multipart/form-data with file + JSON in Mendix, you must use a custom Java action to parse HttpServletRequest parts, then manually store the file and process metadata. This is the standard and reliable approach.


answered
0

Dear Guillaume,


I've implemented the same in my projects. Create form input as filedocument for file and form input as string for json. I've also written a blog on this Publishing multipart/form-data REST Service API in Mendix, thanks to your question.

Hope my answer helps. Reach me out for any queries.

answered