ScritpUnit and JSON data

12 Mar '13, 06:32 PM
No Forum Badges

Hi, I'm trying to write in a ScriptUnit an HTTP get with JSON.

I have this example from

How can I pass response out of ScriptUnit?

This is the code:

    module='http-builder', version='0.5.2' )

def http = new HTTPBuilder( '' )

http.get( path: 'user_timeline.json',
        query: [id:'httpbuilder', count:5] ) { resp, json ->

    println resp.status

    json.each {  // iterate over JSON 'status' object in the response:
        println it.created_at
        println '  ' + it.text

x 0
Answer Answer at this question and get points!
No Forum Badges

The most basic way of returning outputs from a Scirpt Unit is to just use the return statement.

For example, you may extract data from your JSON structure and turn it into a list of Groovy maps, and then return the entire list.

def result = []
def http = new HTTPBuilder('')
http.get(path: 'user_timeline.json', query: [id:'httpbuilder', count:5] ) { resp, son ->
    def entry = [""created"": it.created_at, ""text"": it.text]
return result

Another possibility, if your data is ""flat"", is returning a series of related lists, each one including a piece of data.

#output String[] creationTimes
#output String[] texts
def creationTimes = []
def texts = []
def http = new HTTPBuilder('')
http.get(path: 'user_timeline.json', query: [id:'httpbuilder', count:5] ) { resp, son ->
return [""creationTimes"": creationTimes, ""texts"", texts]

This will cause your Script Unit to output two parameters that can be conveniently coupled to two attributes, for example of a Create Unit for storing data on database. For more information about the #output syntax, please see the wiki article introducing the Script Unit (section Declaring typed inputs and outputs).

x 2
No Forum Badges

I'm very sad, there is a regression update.

Since this summer, Twitter API were updatet to version 1.1 and my code doesn't work anymore.

I need to add the autentication before to call the search. I created my own authentication into Twitter portal and found some code examples for application authentication, but this not work.

OAuthConsumer consumer = new DefaultOAuthConsumer(
"<my Key>",
"<my Secret>");

OAuthProvider provider = new DefaultOAuthProvider(

String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
String pinCode = "<my PIN>";
provider.retrieveAccessToken(consumer, pinCode);

consumer.setTokenWithSecret(consumer.getToken(), consumer.getTokenSecret());

URL url = new URL("");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
int statusCode = request.getResponseCode();

http.request('' , GET, JSON ) {
    uri.path = '/1.1/search/tweets.json'
    uri.query = [q: i_query ]

Any idea?

Tks, Alessio.

[ WebRatio Personal Version: 7.0.5 Build id: 201305081004 ]

x 0
Answer at this question and get points!