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,293,786
Community Members
 
Community Events
165
Community Groups

Post JSON to Web API using Adaptivist Script Runner

I am trying to create a post to a web API that will be used to create Active Directory users, but I cannot seem to get the formatting right. I have tried multiple searches and web links but am struggling to come across a viable option.

 

I have asked a similar question and got some response, but the issue is still present. So I have taken a different approach.

 

Basically I am reading user defined fields on a Jira SSD and trying to formulate the JSON accordingly, this is what I have so far:

 

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;
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper
import groovy.json.JsonOutput

// Define Required Component Access

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

// Get Issue ID that contains the Data
Issue issue = issueManager.getIssueObject( "SSD-18222" );

// Get field values
def fullName = customFieldManager.getCustomFieldObjectByName("Full Name");
def fullNameValue = issue.getCustomFieldValue(fullName);
//def firstName = customFieldManager.getCustomFieldObjectByName("Firstname");
//def firstNameValue = issue.getCustomFieldValue(firstName);
def firstNameValue = "Jane"
def lastName = customFieldManager.getCustomFieldObjectByName("Surname");
def lastNameValue = issue.getCustomFieldValue(lastName);
//def officeNumber = customFieldManager.getCustomFieldObjectByName("Office Number");
//def officeNumberValue = issue.getCustomFieldValue(officeNumber);
def officeNumberValue = "+27115234020"
def mobileNumber = customFieldManager.getCustomFieldObjectByName("Cell Phone Number");
def mobileNumberValue = issue.getCustomFieldValue(mobileNumber);
//def faxNumber = customFieldManager.getCustomFieldObjectByName("Office Number");
//def faxNumberValue = issue.getCustomFieldValue(faxNumber);
def title = customFieldManager.getCustomFieldObjectByName("Job Title");
def titleValue = issue.getCustomFieldValue(title);
def department = customFieldManager.getCustomFieldObjectByName("Department");
def departmentValue = issue.getCustomFieldValue(department);
def siteName = customFieldManager.getCustomFieldObjectByName("Site Code");
def siteNameValue = issue.getCustomFieldValue(siteName);
def idNumber = customFieldManager.getCustomFieldObjectByName("ID Number");
def idNumberValue = issue.getCustomFieldValue(idNumber);
//def passportNumber = customFieldManager.getCustomFieldObjectByName("Passport Number");
//def passportNumberValue = issue.getCustomFieldValue(passportNumber);
//def birthdate = customFieldManager.getCustomFieldObjectByName("Birthday");
//def birthdateValue = issue.getCustomFieldValue(birthdate);

// Define JSON

def ContainerPath = """  "LDAP://OU=BU3,OU=BU2,OU=BU1,OU=Employees,OU=contoso,DC=consto,DC=com"  """
def jsonBody = [:]
jsonBody.put("parentcontainerPath", ContainerPath)
jsonBody.put("firstName", firstNameValue)
jsonBody.put("lastName", lastNameValue)
jsonBody.put("telehpone", officeNumberValue)
jsonBody.put("mobile", officeNumberValue)
jsonBody.put("title", titleValue)
jsonBody.put("department", departmentValue)
jsonBody.put("siteName", siteNameValue)
jsonBody.put("identificationNumber", idNumberValue)
jsonBody.put("passportNumber", idNumberValue)
jsonBody.put("birthDate", "idNumberValue")

// API Post
def baseURL = "http://<server>:8090/api/Employees"
URL url = new URL(baseURL);
//URLConnection connection = url.openConnection();
HttpURLConnection connection = url.openConnection() as HttpURLConnection;
connection.requestMethod = "POST"
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.outputStream
connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, jsonBody) }
connection.connect();
log.info ("URL="+url+"Status="+connection.getResponseCode() as String)

 

 If I return jsonBody I see the following:

[parentcontainerPath: "LDAP://OU=BU3,OU=BU2,OU=BU1,OU=Employees,OU=contoso,DC=contoso,DC=com" , firstName:Jane, lastName:Doe, telehpone:+27115555555, mobile:+27115555555, title:Administrator, department:Accounts, siteName:Sandton, identificationNumber:0000000000000, passportNumber:0000000000000, birthDate:idNumberValue]

 

I am trying to format the JSON as follows:

{parentcontainerPath: "LDAP://OU=BU3,OU=BU2,OU=BU1,OU=Employees,OU=contoso,DC=contoso,DC=com" , firstName:Jane, lastName:Doe, telehpone:+27115555555, mobile:+27115555555, title:Administrator, department:Accounts, siteName:Sandton, identificationNumber:0000000000000, passportNumber:0000000000000, birthDate:idNumberValue}

 

My original post is here: https://community.atlassian.com/t5/Jira-questions/Call-external-REST-API-from-workflow-transition/qaq-p/793424#U806235

I have also referenced https://my.usgs.gov/confluence/display/sciencebase/Making+POST%2C+PUT%2C+DELETE+Calls+from+Groovy and https://community.atlassian.com/t5/Answers-Developer-Questions/Making-a-REST-call-using-script-runner/qaq-p/576712

 

all without much success.

 

 

1 answer

1 accepted

Hi

Try this:

Add this import:

import groovy.json.JsonBuilder

 And this line before the comment that reads "API POST"

def finalJSON = new JsonBuilder(jsonBody).toString()).build()

That should output the JSON in the proper format.

Do say if I can help you further.

Cheers!
DYelamos

Thank you for the assist. Managed to get it working. Appreciate the help.

 

I ended up getting the concatenation working as base and it posts successfully. But will look at your code as well for a more robust method I think.

Thanks:

Could you mark this answer as accepted so that other users know that this thread has been answered?

Do say if I can help you further.

Cheers
DYelamos

Hi @Andrew Downs 

I am trying to update a request using the your code

def body = [:]
body.put("IssueNumber","XCAP-688")
body.put("Priority","P4 (No Impact/Notify)")


def baseUrl = new URL("http://xxx/xxx/UpdatePriority");

HttpURLConnection connection = baseUrl.openConnection() as HttpURLConnection;
connection.requestMethod = "POST"
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.outputStream
connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body) }
connection.connect();

log.info ("URL=" +baseUrl +" Status= "+connection.getResponseCode() as String)

 

But it is throwing 500 error(whereas there is no issue with the api.Not sure why is it giving 500 error). If i am trying to pass json data as below I am getting 400 error

def body = """{"Priority": "P4 (No Impact/Notify)", "IssueNumber": "XCAP-688"}"""

 

Can you help me in passing json data

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

222 views 0 6
Join discussion

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