简体   繁体   中英

Trouble parsing URL encoded URI using read(data, "application/x-www-form-urlencoded") function in Dataweave 2.0

I have this url from which I am trying to retrieve the queryParams.

%dw 2.0
output application/json
import * from dw::core::URL
---
{
queryParams : read(parseURI("https://google.com?token=abc%2FL℅2Bxyz%2F").query default "", "application/x-www-form-urlencoded")
}

Output

{
  "queryParams": {
      "token": "abc/L xyz/"
  }
}

Based on url encoding, / is %2F and + is %2B . But in the output I got a space in place of + .

I understand that parseURI is basically decoding the URI to just "token=abc/L+xyz/" , but why does applying a read(data, "application/x-www-form-urlencoded") on top of it change the value to abc/L xyz/ ?

I am trying understand this behavior as I am trying to retrieve the original URI. For that I did the below

encodeURIComponent(read(parseURI("https://google.com?token=abc%2FL℅2Bxyz%2F").query default "", "application/x-www-form-urlencoded").token)

The output for the above line of code is "abc%2F%20xyz%2F" . So %2B is getting converted to %20 .

Firstly there are some issues with your script as shown. It doesn't to work as is because the package import is case sensitive. Also it uses the character instead of the percentage character % for 2B . After fixing those issues what you described can be reproduced.

I changed the script to show both the output of parseURI() and read(). You can see that parseURI() returns a + which is expected. Then using read() with media type "application/x-www-form-urlencoded" you are telling read that the content is to be parsed in urlencoded form format. That implies converting a + into a space character to obtain the actual URL. See this answer about URL encoding for more details.

Using encodeURI() to reencode the output will return %20 instead of + , which seems to be more canonical.

If you absolutely need the original string you may want to use string operations or regular expressions to decompose the query string into the token value.

Example:

Script

%dw 2.0
output application/json
import * from dw::core::URL
var parsed=parseURI("https://google.com?token=abc%2FL%2Bxyz%2F").query
---
{
    parsed : parsed,
    read: read(parsed default "", "application/x-www-form-urlencoded").token,
    encoded: encodeURI(read(parsed default "", "application/x-www-form-urlencoded").token)
}

Output

{
  "parsed": "token=abc/L+xyz/",
  "read": "abc/L xyz/",
  "encoded": "abc/L%20xyz/"
}

Example of obtaining the token using string operations (splitBy()) and some mapping.

%dw 2.0
output application/json
import * from dw::core::URL
var parsed=parseURI("https://google.com?token=abc%2FL%2Bxyz%2F&other=1234").query
---
{
queryParam : parsed splitBy "&" 
    map (  (($ splitBy "=")[0]): ($ splitBy "=")[1] ) 
    reduce ((item, acc={}) -> acc ++ item)
}

Output:

{
  "queryParam": {
    "token": "abc/L+xyz/",
    "other": "1234"
  }
}

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