Groovy HttpBuilder不支持 HTTP PATCH 方法。如何使用一个发出请求?
问问题
1800 次
3 回答
4
由于该方法是作为 Enum 传递的,因此您无法以正常方式添加新方法。幸运的是,它是 Groovy,所以一切皆有可能。我们将替换闭包委托中的 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()
于 2013-05-29T14:15:20.387 回答
0
其他选项 - 使用0.7-SNAPSHOT。
于 2013-06-01T18:49:34.337 回答
0
喜欢 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"
}
于 2016-03-29T16:17:05.463 回答