Hi,
I have an curl command to trigger an external url. Need some help on script runner groovy script for below command format.
curl --location --request POST 'https://XXXX.XXXX.com:443/api/1/rest/slsched/feed/XXXXXXXXXX/projects/SXXXXXX%20xxxxx/JIRA-Access-TEST%20Task' \ --header 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxx' \ --header 'Content-Type: application/json' \ --data-raw '[ { "Request": { "issueKey": "JJIM-26", "issueId": "This is passthorugh response" } } ]'
Thanks in advance.
Regards,
Aditya.D
Hi
You could execute the curl command as a shell command using the "execute()" method available on groovy strings.
def cmd = """curl --location --request POST 'https://XXXX.XXXX.com:443/api/1/rest/slsched/feed/XXXXXXXXXX/projects/SXXXXXX xxxxx/JIRA-Access-TEST Task' \ --header 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxx' \ --header 'Content-Type: application/json' \ --data-raw '[ { "Request": { "issueKey": "JJIM-26", "issueId": "This is passthorugh response" } } ]'"""def process = command.execute()def sOut = new StringBuilder()def sErr = new StringBuilder()process.consumeProcessOutput(sOut,sErr)process.waitForOrKill(60000) //milliseconds... timeout
Then you could process the sOut strings
Or you can use some native html processing classes.
import groovyx.net.http.HTTPBuilderimport groovyx.net.http.Methodimport groovyx.net.http.ContentTypedef httpBuilder = new HTTPBuilder(https://XXXX.XXXX.com:443)def response = httpBuilder.request(Method.POST, ContentType.JSON){ uri.path = "/api/1/rest/slsched/feed/XXXXXXXXXX/projects/SXXXXXX%20xxxxx/JIRA-Access-TEST%20Task" headers.Authorization = "Bearer xxxxxxxxxxxxxxxxxxxx" body = [Request:[issueKey:"JJIM-26", issueId:""]] response.success = { response, json -> return json } response.failure = { resp, data -> log.error " httprequest failed: $data" return null }}
This will give you some native parsing of the response and response error handling etc.
Hi Peter-Dave,
Thanks for the script.
I tried to run the first script, throwing below error:
You can either escape the \ or change the type of groovy string. The dollar-slashy string is perfect for this use case:
(also, I had an error in the script where I called the command string "cmd" but then tried to execute as if I had called it "command"
Here it is fully corrected
def cmd = $/curl --location --request POST 'https://XXXX.XXXX.com:443/api/1/rest/slsched/feed/XXXXXXXXXX/projects/SXXXXXX xxxxx/JIRA-Access-TEST Task' \ --header 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxx' \ --header 'Content-Type: application/json' \ --data-raw '[ { "Request": { "issueKey": "JJIM-26", "issueId": "This is passthorugh response" } } ]'/$def process = cmd.execute()def sOut = new StringBuilder()def sErr = new StringBuilder()process.consumeProcessOutput(sOut,sErr)process.waitForOrKill(60000) //milliseconds... timeout
Thanks again Peter-Dave for valuable script. Could you help me out with two more improvements.
1. First : "Issue key" & "issue id" has to come dynamically. I'm placing this script in post function. As soon status changed, script should pick the "issuekey" and "issuekey id" of that issue.
2. Second : No transaction success information on issue page. Is it possible to print transaction success code/ log info on issue page like activity tab/history/worklog. Some where we can refer as soon as transition is completed.
BR,
It looks like you're new here. Sign in or register to get started.