使用groovy HttpBuilder发出HTTP修补程序查询

使用groovy HttpBuilder发出HTTP修补程序查询,groovy,apache-httpclient-4.x,httpbuilder,Groovy,Apache Httpclient 4.x,Httpbuilder,不支持HTTP修补程序方法。如何使用一个方法发出请求?由于该方法是作为枚举传递的,因此不能以正常方式添加新方法。 幸运的是,它非常棒,所以一切都是可能的。我们将在闭包的委托中替换org.apache.http.client方法: import groovyx.net.http.* import org.apache.http.client.methods.HttpPatch @Grab(group = 'org.codehaus.groovy.modules.http-builder', mo

不支持HTTP修补程序方法。如何使用一个方法发出请求?

由于该方法是作为枚举传递的,因此不能以正常方式添加新方法。 幸运的是,它非常棒,所以一切都是可能的。我们将在闭包的委托中替换org.apache.http.client方法:

import groovyx.net.http.*
import org.apache.http.client.methods.HttpPatch

@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.6')
@Grab(group = 'org.apache.httpcomponents', module = 'httpcomponents-client', version = '4.2')
def runPatch() {
    //serverinfo.groovy just returns the request method
    //Method.DELETE is switched, and won't be used (can't use null, NPE)
    new HTTPBuilder('http://localhost:9090/serverinfo.groovy').request(Method.DELETE) {
        delegate.request = new HttpPatch()
        response.success = { resp, body ->
            assert resp.status == 200
            assert body == 'PATCH'
        }
    }
}

runPatch()

由于该方法是作为枚举传递的,因此不能以常规方式添加新方法。 幸运的是,它非常棒,所以一切都是可能的。我们将在闭包的委托中替换org.apache.http.client方法:

import groovyx.net.http.*
import org.apache.http.client.methods.HttpPatch

@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.6')
@Grab(group = 'org.apache.httpcomponents', module = 'httpcomponents-client', version = '4.2')
def runPatch() {
    //serverinfo.groovy just returns the request method
    //Method.DELETE is switched, and won't be used (can't use null, NPE)
    new HTTPBuilder('http://localhost:9090/serverinfo.groovy').request(Method.DELETE) {
        delegate.request = new HttpPatch()
        response.success = { resp, body ->
            assert resp.status == 200
            assert body == 'PATCH'
        }
    }
}

runPatch()

其他选项-使用。

其他选项-使用。

对于喜欢JAX RS客户端API的用户,解决方案:

def client = ClientBuilder.newClient()
def response = client.target("$baseUrl$restUsersUrl/$userId")
        .request("application/json")
        .header("Authorization", "Basic ${authString}")
        .build("PATCH", Entity.entity(json2Update, MediaType.APPLICATION_JSON))
        .invoke()
if(Response.Status.NO_CONTENT.statusCode == response.status)
{
    println "test"
}

对于喜欢JAX RS客户端API的用户,解决方案如下:

def client = ClientBuilder.newClient()
def response = client.target("$baseUrl$restUsersUrl/$userId")
        .request("application/json")
        .header("Authorization", "Basic ${authString}")
        .build("PATCH", Entity.entity(json2Update, MediaType.APPLICATION_JSON))
        .invoke()
if(Response.Status.NO_CONTENT.statusCode == response.status)
{
    println "test"
}

这将适用于httpcomponents支持但http-builder不支持的任何方法。这将适用于httpcomponents支持但http-builder不支持的任何方法。