Sử dụng cURL Tôi có thể gửi yêu cầu GET với nội dung. Ví dụ:HTTPBuilder set requestBody trên phương thức 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\"}]}"
Đây là JSON trong một định dạng hợp lý vì mức độ dễ đọc của:
{"stuff":"yes",
"listThing":[1,2,3],
"listObjects":[{"one":"thing"},{"two":"thing2"}]}
thường -d
sẽ nói cURL để gửi một POST nhưng tôi đã xác nhận rằng -X GET
được trọng điều đó và nó là gửi GET. Có thể sao chép điều này bằng HTTPBuilder không?
Tôi đã làm:
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
}
Những công trình, nhưng nếu tôi thay đổi .post
-.get
tôi nhận được lỗi:
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
Có cách nào để gửi một GET với một cơ thể yêu cầu sử dụng HTTPBuilder?