How to construct an HTTP post with Groovy HTTPBuilder RESTClient
10,128
Solution 1
Based on the exception it looks like you are using the groovy-wslite library and not HTTPBuilder. If that is the case, the following should work:
def resp = twitter.post(path: 'update.json') {
urlenc status: msg, source:'httpbuilder'
}
Solution 2
Faced a hard time in calling page with httpbuilder which requires login. Hence sharing my working code.
def http = new HTTPBuilder("http://localhost:8080/")
def query = [ username: "testUsername", password:"testPassword"]
http.request(Method.POST,ContentType.URLENC) {
uri.path = "user/signin"
uri.query = query
headers['Content-Type']= "application/x-www-form-urlencoded"
response.success = {resp-> println resp.statusLine }
}
Hope this helps.
Author by
Jonathan Schneider
CEO @Moderne. Broad experience in Java and UI development. Passionate about developer experience and tooling. Formerly Netflix, Spring Team.
Updated on June 04, 2022Comments
-
Jonathan Schneider almost 2 years
The post example from the docs does not function with http-builder 1.7.1.
def msg = "I'm using HTTPBuilder's RESTClient on ${new Date()}" def resp = twitter.post( path : 'update.json', body : [ status:msg, source:'httpbuilder' ], requestContentType : URLENC ) assert resp.status == 200 assert resp.headers.Status assert resp.data.text == msg def postID = resp.data.id
The exception is
wslite.rest.RESTClientException: No such property: body for class: wslite.http.HTTPRequest
Trolling the API, it is not obvious how you are supposed to construct the post correctly. Any ideas?