Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to read xml output data in Jira

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 11, 2019

Hi, I am calling WebService using httpbuilder but unable to read the output data.

Jira server version 7.7.2

I tried using httpbuilder and getting the results. But How I can read the each field values, which are getting response in text/xml format. Please see below code.

 

import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
import groovyx.net.http.HTTPBuilder
import groovy.xml.*
import groovy.util.XmlParser
import groovy.util.XmlSlurper

    
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent

import groovy.sql.Sql
import java.sql.Connection
import java.sql.Timestamp
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface

import org.apache.log4j.Category


def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
// Set the logging level to DEBUG
log.setLevel(org.apache.log4j.Level.DEBUG);

//def String WSDL_URL = "http://URL-links"
def http = new HTTPBuilder( 'http://url-links' )
String soapEnvelope =  """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.ws.pcr.pts.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:prDetails>         
         <arg0>
            <PRNumber>12345</PRNumber>            
            <systemPassword>USERPWD</systemPassword>            
            <systemUserID>USERNAME</systemUserID>
         </arg0>
      </ser:prDetails>
   </soapenv:Body>
</soapenv:Envelope>"""


      http.request( POST ) {  
          headers."Content-Type" = "txt/xml"
             headers."Accept" = "application/soap+xml; charset=utf-8"
             headers."SOAPAction" = "getDetails"
 
           requestContentType = ContentType.XML
          body=soapEnvelope

            response.success = { resp, xml ->  
                       log.debug "${xml}"
                

               //THIS ONE NOT WORKING              
                //def xmlOutput = new XmlParser().parseText(xml)
                //def casenumber = resp.children().find({it.name()=="caseNumber"})?.text()
                //log.debug ""${casenumber}""
                 //log.debug " case number ${xmlOutput.caseNumber.toString()}"
              
            }

            response.failure = { resp, xml ->
                log.debug "fail"
                 }
        }

 

1 answer

0 votes
Szymon Hubar August 3, 2022

Hi, maybe it will be useful to someone. The code gets the xml with the calendar for SLA PowerBox.

 

import java.util.ArrayList
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovy.xml.*


ArrayList regulaDays = []

def remote = new HTTPBuilder("https://jira-test.test")
def issueJson = remote.request(Method.GET) { req ->
uri.path = "/rest/coresoft-calendar/latest/transfer/export/1"


headers.'Authorization' = "Basic api_token" //Change api_token - build string user:password and encode to BASE64

response.success = { resp, xml ->
// log.warn("${xml}")
def xmlBody = new XmlSlurper().parseText("${xml}")
def regularWeekDays = xmlBody.calendar.regularWeekDaysCalendar.entries.stringDate
regularWeekDays.each{
regulaDays.add(it)
}
}
}


log.warn(regulaDays)

 

Suggest an answer

Log in or Sign up to answer