Junior
Junior

Reputation: 43

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

Upvotes: 0

Views: 1021

Answers (3)

Mike W
Mike W

Reputation: 3932

Add the REST client builder plugin

Then use like:

new RestBuilder().get( 'http://localhost:8080/api/Cars' ).json

Upvotes: 0

Christian Esperar
Christian Esperar

Reputation: 528

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);

Upvotes: 0

railsdog
railsdog

Reputation: 1501

String jx = "http://localhost:8080/api/category";
def jsonObject = grails.converters.JSON.parse(jx)

jx is just a character string. Perhaps what you need is to convert that string into a URL, then use URL's getText() method to call that URL and get the response, which is what you want to parse. Perhaps something like:

def jsonObject = JSON.parse(jx.toURL().getText())

Upvotes: 1

Related Questions