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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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
4,555,973
Community Members
 
Community Events
184
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 = 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.

 

 

Thanks and Regards,

Vaishal

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.

1 vote
Pedro Cora
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Jul 11, 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>

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events