Search code examples
groovyhttpbuilder-ng

Trying out HttpBuilder-NG - replacing old HttpBuilder or wslite


I'm attempting to switch to HttpBuilder-NG but having some difficulty with it. I can't seem to find any concrete examples of working with response headers or even the response status code. It was beyond simple with the old httpbuilder - RESTClient to be specific. Also, I use it for testing. Here's one example I need translated to HttpBuilder-NG

def r = client.get(path: '/ws/v1/ping')
assert r.status == 200

what does that look like in HttpBuilder-NG?


Your comment put me on the right track. Thank you! I am hoping to avoid rewriting thousands of lines of test code by making a wrapper class around HttpBuilder-NG so it will return objects similar to the old HttpBuilder/RESTClient. Also, something similar could help people that are trying to get away from wslite. Here's what I've come up with so far, in case it is helpful for other people:

def client = HttpBuilder.configure {
             request.uri = 'https://myServer.server.org'
             request.auth.basic 'user1', 'fakePass1'
             ignoreSslIssues execution
             }
//use the client to make hit the ping endpoint
def r = client.get {
             request.uri.path = path+'ping'
             response.success {FromServer fs, body ->
                        [status: fs.statusCode,
                         headers: fs.headers,
                         data: body
                        ]
             }
        }
assert r.status == 200
assert r.headers.size() > 0
assert r.data[0] == 'pong'

Solution

  • There is a pretty good user guide with many examples.

    Your example could be done in a number of ways, one of which would be:

    HttpBuilder.configure { 
        request.uri = '<YOUR_SERVER>'
    }.get {
        request.uri.path = '/ws/v1/ping'
        response.when(200){
            // ...
        }
    }
    

    See the JavaDocs for more details on the classes and methods referenced.