Cannot handle query parameters in plugin


I'm trying to register custom query parameter in my plugin handler like this one:

public class EssenceRestAction extends BaseRestHandler {

    public EssenceRestAction(Settings settings, RestController controller) {
        controller.registerHandler(GET, "/_hello", this);
        controller.registerHandler(GET, "/_hello/{name}", this);

But when I send request to my custom REST endpoint in the test within this code:

Response response = client.performRequest("GET", "/_hello",
        Collections.singletonMap("name", ids.get(i)), entity);

I've got this:

org.elasticsearch.client.ResponseException: GET http://localhost:9200/_hello?name=005f3370-4626-4309-92a5-def32fae7e87: HTTP/1.1 400 Bad Request
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"request [/_hello] contains unrecognized parameter: [name]"}],"type":"illegal_argument_exception","reason":"request [/_hello] contains unrecognized parameter: [name]"},"status":400}

How can I correctly register my REST endpoint to handle query parameters? Note: I've also tried to register only controller.registerHandler(GET, "/_hello/{name}", this); but got the same error.


just to make sure, which version are you on?

Judging from this error message, you don't read the "name" parameter when processing the incoming request. This is usually done via RestRequest#param(parameterName) somewhere in your implementation of BaseRestHandler#prepareRequest().

Hi, Christoph!

Thanks for your answer. My Elasticsearch version is 5.1.2. I get what was wrong with my code. I was dealing with parameters inside lambda like this one:

protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient nodeClient) throws IOException {
  return channel -> {
    String id = restRequest.param("name");

which leads to the error that I've got above. However if you deal with parameters before lambda all should be fine.

Glad this solved it :wink:

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