Search code examples
authenticationgrailspluginsbasic-authenticationrest-client

how to use get method in groovyx.net.http.RESTClient properly


I'm trying to get JSON file via get method in RESTClient.

Right now I'm trying

def url = 'http://urlurlurl'
def username = 'username'
def password = 'password'
def restClient = new RESTClient(url)
restClient.auth.basic(username, password)

render restClient

When I see what I get from restClient, is just prints

'groovyx.net.http.RESTClient@65333e2e'

Which is hard to understand.

Given that the url is a endpoint of a API get method, and contains JSON file, how can I retrieve JSON file so I can parse it and use that JSON file?

Also I'm trying this too

def url = 'http://urlurlurl'
def username = 'username'
def password = 'password'
def restClient = new RESTClient(url)
restClient.auth.basic(username, password)

//Adding get method
def jsonData = restClient.get(/* what value should I put in here?? */)

This gives me a forbbiden error that says:

Error 500: Internal Server Error
URI: JsonRender
Class: groovyx.net.http.HttpResponseException
Message: Forbidden

Any suggestions? Examples that uses get method in RESTClient will be nice.


Solution

  • The url should be the base url for your api. For example if we want to search some data from an api which complete url is http://localhost:9200/user/app/_search. So, we have base url as http://localhost:9200/ and the path to api is user/app/_search. Now the request looks like this

    def client = new RESTClient( 'http://localhost:9200/' )
    def resp = client.get( path : 'user/app/_search')
    log.debug (resp.getContentAsString())
    

    Hope this will work out.

    Thanks,