Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Making a REST call using script runner

Mike Whitlock July 10, 2014

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

2 answers

Suggest an answer

Log in or Sign up to answer
6 votes
Oded Priva July 13, 2014

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())

amit raj March 10, 2017

thank you for sharing this code, helped me alot!

vaishal June 9, 2017

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

vaishal June 11, 2017

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.

 

 

Thanks and Regards,

Vaishal

nyrret
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 1, 2018

You may need to set a request property for the authorization.  Originally the code was giving me a 400 response code, but I added the following lines to fix the issue:

connection.setRequestProperty("Authorization", "Basic <encrypted credentials>")

I originally tested my request using the Chrome extention "POSTMAN."  The applet allows you to input your username and password credentials and automatically generates the Authorization header for you.   You would then just need to add the header as a request property, as shown above.  Any method of encrypting your credentials should work, however. 

Hope this helps!

Christof Hurst August 28, 2018

Hi, this all works well, but the Response is empty. Running the command in browser or Postman the Response is filled as expected.

What can be the resason?

Thanks for helping.

1 vote
Pedro Cora
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 11, 2014
Mike Whitlock July 13, 2014

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/"

def output = curl.execute().text

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>

TAGS
AUG Leaders

Atlassian Community Events