【发布时间】:2012-03-28 23:06:26
【问题描述】:
使用 cURL 我可以发送带有正文的 GET 请求。示例:
curl -i -X GET http://localhost:8081/myproject/someController/l2json -H "content-type: application/json" -d "{\"stuff\":\"yes\",\"listThing\":[1,2,3],\"listObjects\":[{\"one\":\"thing\"},{\"two\":\"thing2\"}]}"
为了便于阅读,这里是采用合理格式的 JSON:
{"stuff":"yes",
"listThing":[1,2,3],
"listObjects":[{"one":"thing"},{"two":"thing2"}]}
通常-d 会告诉 cURL 发送一个 POST,但我已经确认 -X GET 覆盖它并且它正在发送 GET。是否可以使用 HTTPBuilder 复制它?
我已经完成了:
def http = new HTTPBuilder( 'http://localhost:8081/' )
http.post(path:'/myproject/myController/l2json', body:jsonMe, requestContentType:ContentType.JSON) { resp ->
println "Tweet response status: ${resp.statusLine}"
assert resp.statusLine.statusCode == 200
}
这可行,但如果我将 .post 更改为 .get 我会收到错误消息:
Cannot set a request body for a GET method. Stacktrace follows:
Message: Cannot set a request body for a GET method
Line | Method
->> 1144 | setBody in groovyx.net.http.HTTPBuilder$RequestConfigDelegate
有没有办法使用 HTTPBuilder 发送带有请求正文的 GET?
【问题讨论】:
标签: groovy get request httpbuilder