Using cURL I can send a GET request with a body. Example:
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\"}]}"
Here is the JSON in a reasonable format for legibility's sake:
{"stuff":"yes",
"listThing":[1,2,3],
"listObjects":[{"one":"thing"},{"two":"thing2"}]}
Normally -d
will tell cURL to send a POST but I have confirmed that the -X GET
is overriding that and it is sending GET. Is it possible to replicate this with HTTPBuilder?
I have done:
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
}
Which works, but if I change .post
to .get
I get the error:
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
Is there a way to send a GET with a request body using HTTPBuilder?