4

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 は常に null ですが、Google chrome のポストマン クライアントを使用して同じリクエストを試行すると、期待されるレスポンス ボディが返されます。これは RESTClient の既知の問題ですか?

4

2 に答える 2

8

HTTP Builder のドキュメントによると、データには解析された応答コンテンツが含まれているはずですが、ご存じのとおり、そうではありません。ただし、リーダー オブジェクトから解析済みの応答コンテンツを取得することはできます。これを行うために私が見つけた最も簡単で一貫した方法は、次のように RESTClient オブジェクトにデフォルトの成功クロージャーと失敗クロージャーを設定することです。

def restClient = new RESTClient()
restClient.handler.failure = { resp, reader ->
    [response:resp, reader:reader]
}
restClient.handler.success = { resp, reader ->
    [response:resp, reader:reader]
}

HttpResponseDecorator成功しても失敗しても同じ結果が得られます: 応答 ( のインスタンス) とリーダー (型は応答本文の内容によって決まります)を含む Mapです。

その後、次のように応答とリーダーにアクセスできます。

def map = restClient.get([:]) // or POST, OPTIONS, etc.
def response = map['response']
def reader = map['reader']

assert response.status == 200
于 2014-02-28T20:15:58.983 に答える