How to construct an HTTP post with Groovy HTTPBuilder RESTClient

jkschneider picture jkschneider · Aug 22, 2014 · Viewed 9.5k times · Source

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?

Answer

Sanjay Bharwani picture Sanjay Bharwani · Jul 30, 2015

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.