Nazerke Nazerke - 1 month ago 13x
Groovy Question

Getting json via get request and saving the response in file

Three things I need to accomplish in a gradle task:

1) receive json via get request

2) parse it

3) save it in a file

For the first part I'm trying to use this gradle plugin. I have to put headers like below, but it is not working. I understand in the example given in the github readme for the plugin, header doesn't have quotations, but without them the task isn't executing correct either.

task attack(type: {
httpMethod = 'get'
uri = ''
contentType =
requestHeaders = ["x-client-token": 'ios']
responseHandler = {
println "hello";

For the second I'll use JsonSlurper. inside responseHandler I'll do
new JsonSlurper().parseText(response.text))
isn't it, although I'm not sure where do I find what variable is "response" is in and if that object has "text". Can you please point me to documentation. I'm new to gradle plugins
For the third point, is there a plugin or it could be accomplished by writing groovy script directly inside task?


No plugin is required, Groovy has all you need.

import groovy.json.JsonSlurper

task go << {
    def headers = [ Accept: 'application/json' ]
    def jsonText = new URL( "" ).getText( requestProperties: headers )
    def parsedJson = new JsonSlurper().parseText( jsonText ) as Map
    // use parse Json
    assert parsedJson.ip != null

    new File( 'my-file.json' ).write jsonText