Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Making a REST call using script runner

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

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 =;
def textToTranslate = "";
def targetLanguage = "";
def originalLanguage ="";
def languagesCodes = [
Russian :"RU",

textToTranslate = "text to translate"

targetLanguage = Issue.getCustomFieldValue(targetLanguageObject);

def body_req = [
	"user": user,

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

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,


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;
def log = Logger.getLogger("com.acme.CreateSubtask")
def body_req = [
                ["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();"URL="+url+"Status="+connection.getResponseCode() as String)

Can you please help me to find where i did mistake.



Thanks and Regards,


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!

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.

Thanks Pedro. Here is an update...

I can run a GET just fine and it works...

def curl = "/usr/bin/curl -u admin:admin"

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

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;} {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>

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

330 views 2 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you