简体   繁体   English

DataWeave 中的 URL 编码

[英]URL encoding in DataWeave

I need to hit a service which needs URL encoding in the query parameters and I have the input as below:我需要在查询参数中点击需要 URL 编码的服务,并且输入如下:

{
"test" : ["123", "124"]
}

which when I encode using https://www.urlencoder.io/ I get the below format:当我使用https://www.urlencoder.io/进行编码时,我得到以下格式:

%7B%0A%09%22test%22%20%3A%20%5B%22123%22%2C%20%22124%22%5D%0A%09%7D

and the above I need to pass in the query parameters.而上面我需要传入查询参数。

I try to generate the above URL encoder output in mulesoft with the below dataweave:我尝试使用以下 dataweave 在 mulesoft 中生成上述 URL 编码器输出:

%dw 2.0
output application/x-www-form-urlencoded
---
payload

but it gives me the below output, which is not what I want:但它给了我以下输出,这不是我想要的:

test=123&test=124

So please let me know how I can generate the below pattern in mule for the above input:所以请让我知道如何在 mule 中为上述输入生成以下模式:

%7B%0A%09%22test%22%20%3A%20%5B%22123%22%2C%20%22124%22%5D%0A%09%7D

You are mixing two very different concepts.您正在混合两个非常不同的概念。

application/x-www-form-urlencoded is a MIME Type that is typically used to POST a web form data over HTTP. application/x-www-form-urlencoded是一种 MIME 类型,通常用于通过 HTTP 发布 Web 表单数据。 I mentioned a web form, but technically it can be used send any "JSON like" data and when you do that, it becomes key=value pairs separated by & when there are multiple fields.我提到了一个 Web 表单,但从技术上讲,它可以用来发送任何“类似 JSON”的数据,当你这样做时,当有多个字段时,它变成由&分隔的key=value对。
For Example {"field1": "value1", "field2": "value2"} will become field1=value1&field2=value2 when represented as x-www-form-urlencoded例如{"field1": "value1", "field2": "value2"}在表示为x-www-form-urlencoded时将变为field1=value1&field2=value2

On the other hand, URL Encoding is used to "Percentage Encode" certain characters that are not allowed in URLs (like non ASCII characters) or that have special meaning for URLs (like ? , & ) so that you can safely use it to construct a URL.另一方面, URL Encoding用于对 URL 中不允许的某些字符(如非 ASCII 字符)或对 URL 具有特殊含义的字符(如?& )进行“百分比编码” ,以便您可以安全地使用它来构造一个网址。

What you need is the encodeURIComponent function that you can use to encode your String.您需要的是可用于对字符串进行编码的encodeURIComponent函数。 Also, URL encoding is for Strings not for JSON Objects.此外,URL 编码适用于字符串而不适用于 JSON 对象。 So you will need towrite the JSON payload as String.因此,您需要将 JSON 有效负载write为字符串。 Something like below像下面的东西

%dw 2.0
import encodeURIComponent from dw::core::URL
output application/java
---
encodeURIComponent(
    write(payload, "application/json")
)

Keep in mind you will get different results depending on if you want to keep the indentation or not while writing the payload to String.请记住,根据在将有效负载写入 String 时是否要保留缩进,您将获得不同的结果。 For example this will give you a different (and much shorter) result then the one above.例如,这将为您提供与上述结果不同(且更短)的结果。

%dw 2.0
import encodeURIComponent from dw::core::URL
output application/java
---
encodeURIComponent(
    write(payload, "application/json", {indent: false}) // Shorter URL as it will not keep indentation
)

It is highly preferred not to keep indentations, if you are using this to generate a URL, as it will keep the URLs significantly shorter.如果您使用它来生成 URL,最好不要保留缩进,因为它会使 URL 显着缩短。

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

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