How to configure GET-Response in Groovy using HTTPBuilder

I have several problems stored on my YouTrack server and I would like to extract these problems and pack the information in Strings.

I already worked with RESTClient, however, I had a bad result and, therefore, I want to try another way to get the problems using HttpBuilder and format the xml response in JSON. However, I still do not know how to do this in Groovy (perhaps because I am missing a complete, running example):

Go to this site and this

I would expect my code to look something like this:

def http = new HTTPBuilder('http://www.MyYouTrackServer.com') AuthenticateMe() // I need that, otherwise I cannot access my server http.get( path : 'MyIssue-25', contentType : JSON, query : [???'] ) { resp, reader -> .... // This gap has to be filled somehow, // so that I have a JSONObject or JSONArray, I can work with. .... } println 'Response data: -----' System.out << reader println '\n--------------------' } String str; // this is the important String containing the data 

Any constructive advice, answers or comments are welcome.

Then the answer will look like this:

 <issues> <issue> <comment created="1277899067543" text="is it something wrong?" author="root"/> <field name="numberInProject"><value>0</value></field> <field name="summary"><value>susjs</value></field> <field name="priority"><value>1</value></field> <field name="description"><value>at jsjsjsj.mps.E.java at line 12</value></field> <field name="created"><value>1277392628191</value></field> <field name="updated"><value>1277399118476</value></field> <field name="reporterName"><value>root</value></field> <field name="updaterName"><value>root</value></field> <field name="state"><value>Submitted</value></field> <field name="subsystem"><value>No subsystem</value></field> <field name="fixedInBuild"><value>Next build</value></field> <field name="permittedGroup"><value>All Users</value></field> </issue> </issues> 
+4
source share
2 answers

To achieve your goal, you can use the following approach for working with json:

 import groovyx.net.http.HTTPBuilder def http = new HTTPBuilder('http://www.MyYouTrackServer.com') ... http.get( path : '/MyIssue-25', contentType : 'application/json' ) { resp, reader -> // inside reader you've your json object in `net.sf.json.JSONObject` instance println reader } 

Note that the query parameter of the get() method is optional, this parameter is used for the URL of the request approach, such as https://twitter.com/search?q=asd , for this case the query parameter will be query : [ q : 'asd' ] .

So, back to the code, in the reader object you have an instance of net.sf.json.JSONObject to work, look in its API .

To show a small example, I have a server in http: //localhost/index.json that returns the following json { "a":"a", "b": { "b1":"b1", "b2":"b2" }, "c":"c" } to work using, follow the code:

 import groovyx.net.http.HTTPBuilder def http = new HTTPBuilder('http://localhost') http.get( path : '/index.json', contentType : 'application/json' ) { resp, reader -> // cast the object it not necessary... I cast it // to have the method suggestions by IDE net.sf.json.JSONObject read = reader println read.get("a") // prints "a" println read.get("b").get("b1") // prints "b1" //... // you can also use this approach println read.a // prints "a" println read.b.b1 // prints "b1" println read.b // prints [b1:b1, b2:b2] } 

UPDATE

I read your questions again, it seems, for your description, that you are trying to read the problems from YourTrack in xml format. For this, an approach that really looks like json , in this case the reader object is an instance of GPathResult take a look at the following sample, assuming that your answer will be similar to the one you asked in your question:

 http = new HTTPBuilder('http://www.MyYouTrackServer.com') http.get( path : '/MyIssue-25', contentType : 'application/xml' ) { resp, reader -> // since your response is an xml now in reader you've GPathResult // and now some samples on how to work with the response: // get the text of each <field> def fields = reader.issue.field*.text(); fields.each { print "$it " // prints 0 susjs 1 at jsjsjsj.mps.E.java at line 12 1277392628191 1277399118476 root root Submitted No subsystem Next build All Users numberInProject } // another sample... get the name attribute value for the <field> elements def namesAttr = reader.issue.field* .@name namesAttr.each { print "$it " // prints numberInProject summary priority description created updated reporterName updaterName state subsystem fixedInBuild permittedGroup } // find the <field> value for element which has attribute name="state" def field = reader.issue.'*'.findAll { it.@name == 'state' } println field.text() // prints submitted } 

Also in this YourTrack operation, it seems that there are two query parameters (project and max) to use it, you can add the query parameter to the get() method, that is: query : [ max : '15' ] .

Hope this helps,

+2
source

Here you can convert the XML response to a JSON object. Note. I added an extra problem to the XML response to better show the output.

 import groovy.util.XmlParser import groovy.json.JsonBuilder def text = ''' <issues> <issue> <comment created="1277899067543" text="is it something wrong?" author="root"/> <field name="numberInProject"><value>0</value></field> <field name="summary"><value>susjs</value></field> <field name="priority"><value>1</value></field> <field name="description"><value>at jsjsjsj.mps.E.java at line 12</value></field> <field name="created"><value>1277392628191</value></field> <field name="updated"><value>1277399118476</value></field> <field name="reporterName"><value>root</value></field> <field name="updaterName"><value>root</value></field> <field name="state"><value>Submitted</value></field> <field name="subsystem"><value>No subsystem</value></field> <field name="fixedInBuild"><value>Next build</value></field> <field name="permittedGroup"><value>All Users</value></field> </issue> <issue> <comment created="1277899067543" text="does this work?" author="root"/> <field name="numberInProject"><value>0</value></field> <field name="summary"><value>susjs</value></field> <field name="priority"><value>1</value></field> <field name="description"><value>at jsjsjsj.mps.E.java at line 12</value></field> <field name="created"><value>1277392628191</value></field> <field name="updated"><value>1277399118476</value></field> <field name="reporterName"><value>root</value></field> <field name="updaterName"><value>root</value></field> <field name="state"><value>Submitted</value></field> <field name="subsystem"><value>No subsystem</value></field> <field name="fixedInBuild"><value>Next build</value></field> <field name="permittedGroup"><value>All Users</value></field> </issue> </issues>''' def xml = new XmlParser().parseText(text) def json = new JsonBuilder() json xml.issue.inject([]) {list, issue -> def map = [:] map.comment = [ created: issue.comment["@created"][0], text: issue.comment["@text"][0], author: issue.comment["@author"][0], ] issue.field.each {field -> map[field['@name']] = field.value[0].children()[0] } list << map return list } json.toString() 

The pretty formatted output (thanks to TextMate) of json.toString () is this:

 [ { "comment": { "author": "root", "created": "1277899067543", "text": "is it something wrong?" }, "created": "1277392628191", "description": "at jsjsjsj.mps.E.java at line 12", "fixedInBuild": "Next build", "numberInProject": "0", "permittedGroup": "All Users", "priority": "1", "reporterName": "root", "state": "Submitted", "subsystem": "No subsystem", "summary": "susjs", "updated": "1277399118476", "updaterName": "root" }, { "comment": { "author": "root", "created": "1277899067543", "text": "does this work?" }, "created": "1277392628191", "description": "at jsjsjsj.mps.E.java at line 12", "fixedInBuild": "Next build", "numberInProject": "0", "permittedGroup": "All Users", "priority": "1", "reporterName": "root", "state": "Submitted", "subsystem": "No subsystem", "summary": "susjs", "updated": "1277399118476", "updaterName": "root" } ] 

I have converted XML to what, in my opinion, is a more reasonable representation of the data.

+2
source

All Articles