Search code examples
groovyhttp-headershttpbuilder

Groovy HTTPBuilder get last modified datetime


I am trying to fetch some files from internet. I want to update my schedule service if the last modified time update. With HTTPBuilder I am unable to find the server response with last-modified parameter. Is there any way to get this parameter?


Solution

  • As in the docs Last-Modified is a header and should be searched among other headers. What's important is that it's server that decides if Last-Modified header will be included in the response. Hence, if the server you connect to doesn't return the header in the response it's impossible to get the value.

    Headers can be obtained via response object, see below:

    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1') 
    
    import groovyx.net.http.HTTPBuilder
    import static groovyx.net.http.Method.GET
    import static groovyx.net.http.ContentType.TEXT
    
    def http = new HTTPBuilder( 'http://www.google.com/search' )
    
    http.request(GET,TEXT) { req ->
        response.success = { res ->
            res.headers.each { h ->
                println h
            }
        }
    }