【发布时间】:2013-12-01 05:42:44
【问题描述】:
我正在使用 groovy RESTClient 0.6 发出 POST 请求。我希望响应中有一个 XML 有效负载。我有以下代码:
def restclient = new RESTClient('<some URL>')
def headers= ["Content-Type": "application/xml"]
def body= getClass().getResource("/new_resource.xml").text
/*
If I omit the closure from the following line of code
RESTClient blows up with an NPE..BUG?
*/
def response = restclient.post(
path:'/myresource', headers:headers, body:body){it}
println response.status //prints correct response code
println response.headers['Content-Length']//prints 225
println response.data //Always null?!
response.data 始终为空,即使当我使用 Google chrome 的邮递员客户端尝试相同的请求时,我也会得到预期的响应正文。这是 RESTClient 的已知问题吗?
【问题讨论】:
-
发出 POST 请求时
{it}是什么? -
@dmahapatro RESTClient.post 方法接受一个闭包作为最后一个参数。您可以发送代码来处理闭包中的响应。有趣的是,如果你不使用接受闭包的 post 方法的重载,post 方法就会爆炸。 :-(
标签: groovy httpbuilder