[英]Issue HTTP PATCH query with groovy HttpBuilder
Groovy HttpBuilder does not support HTTP PATCH method. Groovy HttpBuilder不支持HTTP PATCH方法。 How can I issue a request using one?
如何使用一个请求发出请求?
Since the method is passed as Enum, you can't add new methods in a normal way. 由于该方法作为Enum传递,因此无法以正常方式添加新方法。 Luckily, it's Groovy, so everything is possible.
幸运的是,它是Groovy,所以一切皆有可能。 We'll replace org.apache.http.client method in the closure's delegate:
我们将在闭包的委托中替换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()
其他选择 - 使用0.7-SNAPSHOT 。
Solution for those, who prefer JAX RS Client API: 那些喜欢JAX RS Client 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"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.