Hi. I need to make a REST call to another application from JIRA. The call needs to do a post of json data. I have spent all day googling this and can't come up with anything. Does someone have an example of where they are posting json in a groovy script? I am trying to do this in script runner. Thanks in advance. Mike
Hi Mike,
I've found https://answers.atlassian.com/questions/178254/calling-rest-api-from-within-script-runner-post-function-and-needed-librariesthat might help you a little, at least to start.
Thanks Pedro. Here is an update...
I can run a GET just fine and it works...
def curl = "/usr/bin/curl -u admin:admin http://itest.com/jira/rest/api/2/issue/ITCMTEST-245"
def output = curl.execute().text
However, when I run the POST below, I get the following errors (I think it has to do with the double quotes and single quotes... I tried to escape them but get error '415 - Unsupported Media Type'... has someone been able to get this to work if so PLEASE help)...
def curl = "/usr/bin/curl -u admin:admin -X POST -d \'{\"fields\":{\"project\":{\"id\":\"12660\"},\"summary\":\"MIKE\",\"description\":\"BART\",\"issuetype\":{\"id\":\"76\"}}}\' -H \"Content-Type: application/json\" http://itest.com/jira/rest/api/2/issue/"
ERROR = <html><head><title>Apache Tomcat/7.0.29 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 415 - Unsupported Media Type</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Unsupported Media Type</u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type).</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.29</h3></body></html>
Here is the script I'm using to POST a translate text to translation service.
import groovy.json.JsonSlurper; import groovy.json.StreamingJsonBuilder; import com.atlassian.jira.ComponentManager; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.MutableIssue import org.apache.commons.codec.binary.Base64; def user = "jira"; def issueID = ""; IssueManager issueManager = ComponentManager.getInstance().getIssueManager(); //MutableIssue Issue = issueManager.getIssueObject("DEMO-467"); MutableIssue Issue = issue; issueID = Issue.getKey(); def issueType = Issue.issueTypeObject.name; def textToTranslate = ""; def targetLanguage = ""; def originalLanguage =""; def languagesCodes = [ English:"EN", Spanish:"ES", Franch:"FR", German:"DE", Polish:"PL", Russian :"RU", Japanese:"JP", Portugese:"PT", Italian:"IT", Dutch:"NL", Turkish:"TR", Czech:"CZ", ] textToTranslate = "text to translate" targetLanguage = Issue.getCustomFieldValue(targetLanguageObject); def body_req = [ "user": user, "jiraTicket":issueID, "route":"/resources/text", "targetLang":languagesCodes[targetLanguage], "reqText":textToTranslate ] def baseURL = "http://production.******.com/api/translation"; URL url; url = new URL(baseURL); URLConnection connection = url.openConnection(); connection.requestMethod = "POST" connection.doOutput = true connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8") connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) } connection.connect(); println("url: " + url); println("Content:" + connection.getContent()) println("ResponseCode:" + connection.getResponseCode()) println("getResponseMessage:" + connection.getResponseMessage())
thank you for sharing this code, helped me alot!
Hi Amit
Is this script working for you?
because I am trying to create Index in Elasticsearch on ticket creation but it is throwing 400 error code.
And same script is working perfect in Jenkins.
Thanks and Regards,
Vaishal
Hi Priva,
I am trying to create Index in Elasticsearch using REST API with same code but it won't be work.
Please find my code
import org.apache.log4j.Logger import org.apache.log4j.Level import groovy.json.JsonSlurper; import groovy.json.StreamingJsonBuilder; import org.apache.commons.codec.binary.Base64; //Debug def log = Logger.getLogger("com.acme.CreateSubtask") log.setLevel(Level.DEBUG) //Body def body_req = [ "settings": ["index": ["number_of_shards" : 3, "number_of_replicas" : 2] ] ] //Create Index with Jiraicket Name def baseURL = "http://localhost:9200/JiraTicket"; URL url; url = new URL(baseURL); HttpURLConnection connection = url.openConnection() as HttpURLConnection; connection.requestMethod = "POST" connection.doOutput = true connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8") connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) } connection.connect(); log.info("URL="+url+"Status="+connection.getResponseCode() as String)
Can you please help me to find where i did mistake.
It looks like you're new here. Sign in or register to get started.