我有如下所示的curl命令,我需要在Groovy脚本中为Jenkins管道运行相同的命令。如何实现多个网址编码?
curl --location --request POST 'https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=234htryjbd97bkjkjkls0099kjkbjb' \
--data-urlencode 'client_secret=9987657884yterws87654hjkjhhvj' \
--data-urlencode 'grant_type=client_credentials'
我对詹金斯管道使用groovy与body一样:
def jsonBody = '{"userName": "' + myusername +'" ,"password": "' + mypassword + '" }'
def token = httpRequest consoleLogResponseBody: false, contentType: 'APPLICATION_JSON', httpMode: 'POST', httpProxy: 'http:// myproxy.someone.com:8080', requestBody: jsonBody, url: 'https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token', wrapAsMultipart: false
def cont = new JsonSlurper().parseText(token.content)
根据m子文档,oauth / token请求可以是纯json:
POST /accounts/oauth2/token HTTP 1.1
Host: anypoint.mulesoft.com
Content-Type: application/json
{
"client_id" : "123456789",
"client_secret": "123456789",
"grant_type" : "client_credentials"
}
因此,您的代码可能像这样:
def jsonBody = new groovy.json.JsonBuilder(
client_id: '234htryjbd97bkjkjkls0099kjkbjb',
client_secret: '9987657884yterws87654hjkjhhvj',
grant_type: 'client_credentials'
).toPrettyString()
def token = httpRequest(
consoleLogResponseBody: false,
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
httpProxy: 'http:// myproxy.someone.com:8080',
requestBody: jsonBody,
url: 'https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token',
wrapAsMultipart: false
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句