rahulserver
rahulserver

Reputation: 11205

Grooviest way to send HTTP POST with JSON payload in Grails

I know how to send an HTTP POST with JSON payload using Apache HTTP library or other conventional java ways. But is there a groovier or in fact grooviest way to do so in grails?

I want a simplistic solution with minimum lines of code(=beauty of groovy/grails).

EDIT I don't want to use any third party library. I am looking for a pure grails way to do it. Also the http post should be synchronous(blocking) not asynchronous.

Upvotes: 2

Views: 7022

Answers (2)

Opal
Opal

Reputation: 84786

You can use Jodd. E.g.:

def response = HttpRequest
        .post('http://srv:8080/api/jsonws/user/get-user-by-id')
        .form('userId', '10194')
        .send()

Sending a JSON:

def resourcePost = request().
          method(POST.toString()).
          path('http://some.url.com/api').
          body('{"value":1}').
          send()

Upvotes: 3

John McClean
John McClean

Reputation: 5313

HttpBuilder is expressive and powerful / extensible

  new HTTPBuilder(queryUrl).request(Method.POST, ContentType.JSON){
          body = payload
          response.success = { streamResponse, reader ->
               result = reader.readLines().join()
          }
   }

Upvotes: 2

Related Questions