【问题标题】:Passing parameter inside the groovy script in mule4在 mule4 的 groovy 脚本中传递参数
【发布时间】:2020-05-25 23:57:42
【问题描述】:

我正在尝试通过 mule 中的 .properties 文件参数化用户名和密码。 groovy 脚本有 curl 命令。我在执行组件的参数部分定义了 VID 和 VPASS,并将其传递到脚本中,如下所示。在运行时,它无法解析 id 和密码。如何在 curl 命令内部传递参数。

def proctext = 'curl -H "apiAccessKeyId: + vid" -H "apiSecretAccessKey**:+vpass**" -H "Accept:application/json" --form "file= @C:\Files\test.csv" --form "params={Type:Import}" -X POST http://mysuperserver/media/upload/'

【问题讨论】:

  • stackoverflow.com/questions/61928860/… 中,您要求提供非常相似的内容,并且接受的答案指出,使用 curl 是一个坏主意。
  • 这种方法似乎不错,但对我来说并不奏效,我在那里遇到了一些问题。尝试其他可能的解决方案
  • 我建议您写一个问题,详细说明其他答案无效的原因。
  • 好的@aled。我会在某个时候发布问题
  • 但是我能得到答案吗?对我来说这将是有用的学习。在此先感谢

标签: curl groovy mule mule4


【解决方案1】:

您必须将parameters element of the Scripting Module 中的参数设置为DataWeave 表达式。

<flow name="groovy-paramsFlow" >
    <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/"/>
    <set-variable value="#[123]" doc:name="Set Variable" variableName="myVar"/>
    <scripting:execute doc:name="Execute" engine="groovy">
        <scripting:code ><![CDATA[def proctext = 'curl -H "apiAccessKeyId: ' + vid + '" -H "apiSecretAccessKey: ' + vpass +'" -H "Accept:application/json" --form "file=@C:\\Files\\test.csv" --form "params={Type:Import}" -X POST http://mysuperserver/media/upload/']]></scripting:code>
        <scripting:parameters ><![CDATA[#[{ vid: "abcd", vpass: "efgh" }]
        ]]></scripting:parameters>
    </scripting:execute>
    <logger level="INFO" message="Payload #[payload]"/>
</flow>

输出

Payload curl -H "apiAccessKeyId: abcd" -H "apiSecretAccessKey: efgh" -H "Accept:application/json" --form "file=@C:\Files\test.csv" --form "params={Type:Import}" -X POST http://mysuperserver/media/upload/

【讨论】:

  • 我能够理解如何将参数传递给脚本。但是在 curl 命令中它无法解析参数的实际值。我怀疑,这可能是因为引号。不是当然
  • 过去我对引号有问题,但这里是反斜杠(``)。请注意,您不仅有 Groovy 语言的问题,还有它与 Mule XML 文件的交互问题。还有那些不在字符串中的参数,所以它们是文字而不是变量。我已经更新了我的示例以匹配您的示例。提示:始终在您的问题中填写完整的错误消息。他们在很多情况下都有线索。
【解决方案2】:

Groovy 脚本的参数

有两种方法可以向 Groovy 脚本传入和传出值https://simpleflatservice.com/mule4/ParametersForGroovyScripts.html

一种方法是使用常规变量。在 Groovy 脚本中,它们是变量 vars 的属性。 另一种方法是设置脚本的属性。通过这种方式,变量可以直接使用它们的名称。 出路类似。脚本的最后一条语句在 Groovy 脚本之后成为有效负载。要输出变量值,应更改 vars 的属性。 变量 vars 是不可更改的,因此现在可以添加新属性,但属性本身是可变的并且可以执行任何值。

注意事项 - 默认情况下,Mule 会尝试使用流,并且可能会为交互带来一些困难。例如,如果 Mule 中的变量是 JSON,它可能是一个流,因此不能在 Groovy 脚本中更改流中的任何属性。在这种情况下,最好将变量设置为 Java,然后可以更改属性。

Groovy 脚本组件

全流程

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:db="http://www.mulesoft.org/schema/mule/db"
    xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"

 xmlns:file="http://www.mulesoft.org/schema/mule/file"

 xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:wsc="http://www.mulesoft.org/schema/mule/wsc" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd

http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd

http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/wsc http://www.mulesoft.org/schema/mule/wsc/current/mule-wsc.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

 <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="f7458033-5606-4521-ae2b-7c309c34bdb2" >

  <http:listener-connection host="0.0.0.0" port="8081" />

 </http:listener-config>

 <http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" doc:id="13190c91-83f0-40bf-8288-1f1f330c0b48" >

  <http:request-connection host="localhost" port="8081" />

 </http:request-config>
    <email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="45eb1460-05a9-41c5-80be-344a45b241e0" >
        <email:smtp-connection host="smtp.kclife.net" user="alex.kizub" password="8Abc#135" />
    </email:smtp-config>
    <flow name="workFlow" doc:id="ea9b69db-5516-4940-a81c-b88c3415c75d" >
        <http:listener doc:name="Listener" doc:id="81680002-4376-47d1-ada6-a4f18f978b36" config-ref="HTTP_Listener_config" path="/"/>
        <ee:transform doc:name="set myRegularVariable    and    myCollection" doc:id="e79005e0-6652-4cc6-8f83-807fc0432ed0" >
            <ee:message >
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="myRegularVariable" ><![CDATA[%dw 2.0
output application/java
---
'Hello']]></ee:set-variable>
                <ee:set-variable variableName="myCollection" ><![CDATA[%dw 2.0
output application/java
---
{
    start: now()
}]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <scripting:execute engine="groovy" doc:name="Execute Groovy script" doc:id="b465ab5c-d2b7-4d86-860a-f0097aa30da4" >
            <scripting:code ><![CDATA[def string=vars.myRegularVariable + ' ' + myInScriptVaraiable
vars.myCollection['result']=string
string + '!' // return as payload]]></scripting:code>
            <scripting:parameters ><![CDATA[#[{myInScriptVaraiable:'World'}]]]></scripting:parameters>
        </scripting:execute>
        <logger level="INFO" doc:name="Log payload" doc:id="1f8b99ef-a4ce-42ec-8a2b-0b0e31b4128f" message="#[payload]"/>
        <logger level="INFO" doc:name="Log myCollection" doc:id="2cdd252d-3993-4dd5-b897-2456761ad293" message="#[vars.myCollection]"/>
    </flow>

</mule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2020-11-27
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多