4

My current payload is (received from a POST)

<?xml version='1.0' encoding='ISO-8859-1'?>
<test>
</test>

I want to get the encoding value (ie ISO-8859-1)

What is the correct DataWeave expression to use ?

I already tested

var infos = payload.^mediaType splitBy "; "
var encoding = infos[1] splitBy  "="
---
media:
{ 
    mime: infos[0],
    encoding: encoding[1]
}

But it return me:

{
"mime": "application/xml",
"encoding": "UTF-8"
}

It seems payload.^mediaType is coming from my POST headers.

Here is the solution to the problem:

%dw 2.0
output application/java
---
((payload.^raw as String) scan /encoding='([A-z0-9-]+)'/)[0][1]

In the proposed solution the ^ symbol is missing.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

9

Sadly there is no elegant way to do this. The only way I thought is by using regex.

%dw 2.0
output application/java
---
((payload.^raw as String) scan /encoding='([A-z0-9-]+)'/)[0][1]

This is going to return the encoding.

machaval
  • 4,969
  • 14
  • 20