Skip to content
Advertisement

Get key-value pairs from multipart/form-data HTTP POST request in Camel

I have an endpoint setup using Apache Camel to receive a multipart/form-data HTTP request. Essentially I am trying to submit a data file and a configuration file for processing. The request is as follows (generated by Postman):

JavaScript

My route is set up like so:

JavaScript

And my config file processor:

JavaScript

I want to be able to retrieve the key-value pairs from the form-data and process the data and config files accordingly. Instead I get the following behaviour:

  • The first value in the form (in this case data-file.json) is parsed into the Camel message body, and the key seems to be discarded. The rest of the entries are parsed into an AttachmentMessage. This behaviour is documented here https://stackoverflow.com/a/67566273/11248602
  • The keys in the AttachmentMessage are not the original keys from the form-data request, but the filenames (e.g. config-file.json)

Is there any way to parse this request into a map or similar structure so that all the original keys and values can be accessed?

Advertisement

Answer

For the sake of clarity, as the proposed solution here above seems to work, a bit more explanations (although it is more a workaround than a solution):

Starting from:

JavaScript

It is possible to browse all map entries, and for each found Attachment object, examine the attachment headers using getHeaderNames(), among other the ‘Content-Disposition’ one:

JavaScript

which can finally be parsed to grab the form name (‘data’ in this example).

Not straightforward true, but this apparently works…

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement