user3138596
user3138596

Reputation: 71

grails http post request with JSON data

I am trying to send a POST request using grails. I have read various post already present but no success. I have done following stuff till now.

  1. Created a new grails project using GGTS.
  2. Created a controller
  3. Installed Rest plugin for grails.
  4. Made below changes in my grails controller.

    def index(){
     withHttp(uri: "http://127.0.0.1:3001") {
       def html = post(query : [q:'Groovy'])
       assert html.HEAD.size() == 1
       assert html.BODY.size() == 1
        }}
    
  5. when I am trying to access the controller it breaks.

Am I doing anything wrong here. I am sending JSON data in POST request which would later be saved? Any pointers directions would be very helpful.

Upvotes: 1

Views: 7313

Answers (1)

Emmanuel John
Emmanuel John

Reputation: 2325

I have not tried doing this in a Grails controller but I figure since its groovy, it should work.

import groovyx.net.http.*
def http = new HTTPBuilder( 'http://127.0.0.1:3001' )
http.request( POST, JSON ) { req ->
     body = [q:'groovy']

     response.success = { resp, json ->
     // handle repsonse
     }
}

See more at: http://groovy.codehaus.org/modules/http-builder/doc/json.html and Groovy HTTPBuilder POST: missing method(s)

Upvotes: 1

Related Questions