Search code examples
groovygaelykgroovlet

Gaelyk: returned truncated JSON


I am "piping" a json feed (in some cases quite big) that is returned from an external service, to hide an access api-key to the client (the access key is the only available authentication system for that service).

I am using Gaelyk and I wrote this groovlet:

try {
    feed(params.topic)
} catch(Exception e) {
    redirect "/failure"
}

def feed(topic) {

    URL url = new URL("https://somewhere.com/$topic/<apikey>/feed")
    def restResponse = url.get()

    if (restResponse.responseCode == 200) {
        response.contentType = 'application/json'
        out << restResponse.text
    }
}

The only problem is that the "restResponse" is very big and the value returned by the groovlet is truncated. So I will get back a json like this:

[{"item":....},{"item":....},{"item":....},{"ite

How can I return the complete json without any truncation?


Solution

  • Well I found the solution and the problem was at the beginning (the URL content must be read as stream). So the content truncated it was not the output but the input:

    def feed(topic) {
        URL url = "https://somewhere.com/$topic/<apikey>/feed".toURL()
        def restResponse = url.get()
    
        if (restResponse.responseCode == 200) {
            response.contentType = 'application/json'
            StringBuffer sb = new StringBuffer()
            url.eachLine {
                sb << it
            }
            out << sb.toString()
        }
    }