简体   繁体   English

试用HttpBuilder-NG-替换旧的HttpBuilder或wslite

[英]Trying out HttpBuilder-NG - replacing old HttpBuilder or wslite

I'm attempting to switch to HttpBuilder-NG but having some difficulty with it. 我正在尝试切换到HttpBuilder-NG,但遇到了一些困难。 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. 特定于旧的httpbuilder-RESTClient并不简单。 Also, I use it for testing. 另外,我将其用于测试。 Here's one example I need translated to HttpBuilder-NG 这是我需要翻译为HttpBuilder-NG的一个示例

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

what does that look like in HttpBuilder-NG? 在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. 我希望通过围绕HttpBuilder-NG制作一个包装器类来避免重写数千行测试代码,这样它将返回类似于旧HttpBuilder / RESTClient的对象。 Also, something similar could help people that are trying to get away from wslite. 同样,类似的东西也可以帮助那些试图摆脱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'

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. 有关所引用的类和方法的更多详细信息,请参见JavaDocs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM