简体   繁体   English

在 Mule4 中无法识别 DataWeave Multipart/form-data 输入编码

[英]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?是否可以选择在接收到侦听器的 multipart/form-data 中按部分设置编码?

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: JSON 文件都是 UTF-16 格式,所以当我尝试将文件数据存储到变量时 - 使用以下 DataWeave 脚本:

%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.所以我认为 DataWeave 只是尝试使用 UTF-8 编码而不是 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.我还尝试在使用 DataWeave 的Set 变量组件上设置编码(如下面的代码片段所示),但这并没有改变任何东西。

<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.从错误中提到的字符ÿ看来,它是数据开头的字节顺序标记 (BOM)的一部分,以指示 UTF 编码。 DataWeave doesn't currently support BOMs and thinks it is garbage, so it throws an error. DataWeave 目前不支持 BOM 并认为它是垃圾,因此会引发错误。 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.如果您无法避免该有效负载与 BOM 一起发送,您可能需要一些 Java 方法或脚本将其删除,然后再尝试使用 DataWeave 对其进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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