Groovy HttpBuilder не поддерживает метод HTTP PATCH. Как я могу отправить запрос с его помощью?
Выполните запрос HTTP PATCH с помощью groovy HttpBuilder
Ответы (3)
Поскольку метод передается как 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()
person
JBaruch
schedule
29.05.2013
Это будет работать для любого метода, поддерживаемого httpcomponents, но не для http-builder.
- person JBaruch; 01.06.2013
Решение для тех, кто предпочитает 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"
}
person
Alexandr
schedule
29.03.2016