Use Unirest in Scriptrunner for Server

Hi,

I've been doing some integration with JIRA and ServiceNow and was using an JIRA cloud dev environment for my initial testing where everything was working great.

I have now transitioned to a local server version of JIRA and it seems like the Unirest.post function is not defined by default in this version. I was wondering if there is an import I can make to get it to work or if I need to use a different method.

def statusresponse = Unirest.post("https://xxxxx.service-now.com/api/now/table/change_request")
.basicAuth("xxxx", "xxxxx")
.header("Accept","application/json")
.header("Content-Type","application/json")
.body("{\"u_subcategory\":\""+iss+"\",\"category\":\"Jira Integration\",\"short_description\":\""+desc+"\",\"correlation_id\":\""+key+"\",\"correlation_display\":\"Jira Integration\"}")
.asJson();

 

This is the code that was working on the cloud version but now gives an error in the server version.

1 answer

1 accepted

0 vote
Alexey Matveev Community Champion Jan 03, 2018

Hello,

Kindly have a look at this link (there is an example of how you can do it)

https://community.atlassian.com/t5/Answers-Developer-Questions/Making-a-REST-call-using-script-runner/qaq-p/576712

Using that method and I'm able to send the POST request. But with Unirest after running the Unirest.post function it returns the body of the response :

POST https://devxxxx.service-now.com/api/now/table/change_request asJson Request Duration: 4422ms
status: 201 - Created
body: {"result":{"parent":"","made_sla":"true","caused_by":"",....}}

 I seem to be unable to retrieve the response body using HttpURLConnection

Alexey Matveev Community Champion Jan 04, 2018

I think connection.getResponseMessage() should get the response.

connection.getResponseMessage() was returning sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@69c728b9

 

I had to buffer the input stream to read it properly:

def inputStream = connection.getInputStream();

BufferedReader input = new BufferedReader(
new InputStreamReader(
inputStream));

StringBuilder response = new StringBuilder();
String currentLine;

while ((currentLine = input.readLine()) != null)
response.append(currentLine);

input.close();
response.toString();

 This gave me the same output as using Unirest.

 

Thanks for the help!

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

81 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you