Skip to content

Instantly share code, notes, and snippets.

@zedar
Last active August 29, 2015 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zedar/abbb3c01635c0d6a977f to your computer and use it in GitHub Desktop.
Save zedar/abbb3c01635c0d6a977f to your computer and use it in GitHub Desktop.
HTTPBuilder and chunked response

There is a bug in Groovy version up to 2.3.7. HTTPBuilder is not able to automatically parse chunked response to JSON format. If external server returns response with header:

Transfer-Encoding : chunked

Usually applied code:

def http = new HTTPBuilder(url)
def result = http.request(POST, JSON) { req ->
    body = inputData
    
    response.success = { resp, json ->
        ...
    }
    response.failure = { resp ->
        ...
    }
}

change to:

def http = new HTTPBuilder(url)
def result = http.request(POST, JSON) { req ->
    body = inputData
    
    response.success = { resp ->
        String text = resp.entity.content.text
        String contentType = resp.headers."Content-Type"
        if (contentType?.startsWith("application/json")) {
            def json = JsonSlurper().parseText(text)
            ...
        }
        else {
            // return error
            ...
        }
    }
    response.failure = { resp ->
        ...
    }
}

The workaround gets whole data before JsonSlurper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment