Jonathan Schneider
Jonathan Schneider

Reputation: 27727

How to construct an HTTP post with Groovy HTTPBuilder RESTClient

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?

Upvotes: 2

Views: 11238

Answers (2)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4759

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.

Upvotes: 3

John Wagenleitner
John Wagenleitner

Reputation: 11035

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'
}

Upvotes: 2

Related Questions