Groovy REST client unable to get JSON response when using search API

I’m trying to use Groovy’s RESTClient to connect to Graylog server and use it’s REST API. I want a JSON response from Graylog.

This is my RESTClient (Graylog is running on my local machine)

  def restClient = new RESTClient("http://localhost:9000/api/")
    restClient.auth.basic 'admin', 'admin'

    def response = restClient.get(
        path:"search/universal/relative?query=hello&range=50000&fields=message&decorate=true",
        headers:[
            "Accept": "application/json"
        ]
    )

It’s a simple rest client trying to search for a word “hello” in Graylog’s messages. I keep getting 404 Not Found exception

If I use the following curl command, I do get my JSON response.

curl --user admin:admin -H ‘Accept: application/json’ -X GET “http://localhost:9000/api/search/universal/relative?query=hello&range=50000&fields=message

I tried the same REST API call using Postman and am able to get a JSON response back.

What did I miss in my groovy rest client that is causing it to receive a HttpResponseException?

I noticed that If I change the “path” parameter to “streams”, everything works just fine. But, I can’t seem to call the search API successfully.

Graylog version is 2.2.3-1

You might want to take another look at the URI you’re building.

I’m pretty sure that the part after the first question mark is not the URI path component. :wink:

That it!! It was because of the querystring being included in the path. The rest client has separate parameter for that. Here’s how it should be. Thanks a lot.

def restClient = new RESTClient("http://localhost:9000/api/")
restClient.auth.basic 'admin', 'admin'

def response = restClient.get(
    path:"search/universal/relative",
    query: [
        "query" : "hello",
         "range" : 50000,
         "fields" : "message"
    ],
    headers:[
        "Accept": "application/json"
    ]
)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.