使用 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?