Search code examples
restgrailspostrestful-urlhttpbuilder

Grails how to write a RESTful post call for my grails application


I am new in writing RESTful calls, and I am also new in Grails. However I am trying to write my first Rest API in my Grails application.

Based on this link: http://grails.github.io/grails-doc/3.0.10/guide/webServices.html#versioningResources

I noticed that there is fully explanation of how to make a get and post request.

However, I would like to make a rest post call through my code, and not via culr Unix tool.

What should I use? I am running Grails 3.0.10, can I use HttpBuilder?

Can you provide me examples?


Solution

  • Research Grails Rest Builder, which is part of the Grails REST API. For example:

    import grails.plugins.rest.client.RestBuilder
    // POST request and get response back
    def resp = new RestBuilder().post("$baseUrl/api/employees/count")
    // confirm that status and returned values are ok
    resp.status == OK.value()
    resp.json.count == 100
    

    The groovydocs are here: http://springsource.github.io/grails-data-mapping/rest-client/api/index.html. Older documentation is here: https://github.com/grails-plugins/grails-rest-client-builder/.