Search code examples
grailsgrails-plugingrails-2.0grails-controllergrails3

Json calling rest url to grails gsp page


I have tried to find an example who shows my how to render a rest url like this : http://localhost:8080/api/Cars in a gsp page.

I have tried every example people provides. No one of them is totally correct or its not working for my.

Some of the things i have tried is grails.converters.JSON.parse, Jsonslurper, HTTPBuilder.

Can someone give me a step by step introduction on how to just make a call from this url: "http://localhost:8080/api/Cars" to an controller who renders the json to gsp page.

By the way im using grails 3.

Thanks in advance


Solution

  • You can do something like this

    def cars
    def url = 'http://localhost:8080/api/Cars'
    
    def httpClient = HttpClients.createDefault()
    HttpGet httpGet = new HttpGet(url)
    def response = httpClient.execute(httpGet)
    
    cars = EntityUtils.toString(response.getEntity())
    cars = new JsonSlurper().parseText(cars);