简体   繁体   中英

DataWeave Multipart/form-data input encoding not recognized in Mule4

Is there an option to set encoding by part in multipart/form-data received to listener?

I have this kind of multipart to be received:

----------------------------180928595588258919887097
Content-Disposition: form-data; name="qualifyResult"; filename="json1.json"
Content-Type: application/json

{
    "json1": "1"
}

----------------------------180928595588258919887097
Content-Disposition: form-data; name="raceOneResult"; filename="json2.json"
Content-Type: application/json

{
    "json2": "2"
}

----------------------------180928595588258919887097--

Both JSON files are in UTF-16, so when I try to store file data to variable - using the following DataWeave script:

%dw 2.0
output application/json
---
payload.parts.'qualifyResult'.content

It returns this kind of error:

org.mule.runtime.core.api.expression.ExpressionRuntimeException: "Unexpected character 'ÿ' at qualifyResult@[1:1] (line:column), expected false or true or null or {...} or [...] or number but was , while reading `qualifyResult` as Json.
 
1| 
   ^" evaluating expression: "%dw 2.0

output application/json

---

payload.parts.'qualifyResult'.content".

So I think the DataWeave just tries to read the data using UTF-8 encoding instead of UTF-16. I also tried to set encoding on the Set variable component where I use the DataWeave (as following snippet shows), but this didn't change anything.

<set-variable value="#[%dw 2.0
&#10;output application/json
&#10;---
&#10;payload.parts.'qualifyResult'.content]" doc:name="qualifyResult" doc:id="34800576-0539-4f95-8977-8353a255d83d" variableName="qualifyResult" encoding="UTF-16"/>

How to handle the inbound file data in correct encoding? Thanks for all valid answers.

It doesn't look like problem with the encoding. It seems from the character ÿ mentioned in the error that it is part of a Byte Order Marker (BOM) at the beginning of the data to indicate the UTF encoding. DataWeave doesn't currently support BOMs and thinks it is garbage, so it throws an error. If you can't avoid that payload being sent with a BOM you may need some Java method or a script to remove it before trying to parse it with DataWeave.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM