首页 > 解决方案 > Groovy 声明一个变量

问题描述

我想通过脚本在 HTTP 正文内容中设置以下详细信息。

def clientId ='ddfgertg'
def clientSecret='fghghfghfgh'

我想在下面的字符串变量中设置上面的值

def httpBody = '{ "Client_ID": "${clientId}", "Client_Secret":"${clientSecret}", "Grant_Type":"client_credentials" }'

当我打印 httpBody 时,它没有设置clinetId,clientSecret值。

我的实际输出

{ "Client_ID": "${clientId}", "Client_Secret":"${clientSecret}", "Grant_Type":"client_credentials" }

预期产出

{ "Client_ID": "ddfgertg", "Client_Secret":"fghghfghfgh", "Grant_Type":"client_credentials" }

标签: groovystring-interpolation

解决方案


只需将 ' 转为 """,例如:

def httpBody = """{ "Client_ID": "${clientId}", "Client_Secret":"${clientSecret}", "Grant_Type":"client_credentials" }"""

会没事的。


推荐阅读