0

Hi I am writing custom plugin for elastic search, but I unable to get the parameter from the post request.

    @Inject
public HelloRestHandler(Settings settings, RestController restController, Client esClient) {
    super(settings, restController, esClient);
    restController.registerHandler(RestRequest.Method.GET, "/_hello", this);
    restController.registerHandler(RestRequest.Method.POST, "/_hello", this);
    restController.registerHandler(RestRequest.Method.PUT, "/_hello", this);
}

@Override
public void handleRequest(final RestRequest request, final RestChannel channel, Client esClient) {
    ObjectMapper mapper = new ObjectMapper();

    String json;
    try{json=  mapper.writeValueAsString(request.params());}catch (Exception exp){ json ="not found";}
    channel.sendResponse(new BytesRestResponse(OK,json));}

The curl:

curl -XPOST "http://localhost:9200/_hello/" -d '{"first":"1"}'

response :

"{}" (empty JSON)

Please help me out to fix my issue. Thanks.

Val
  • 207,596
  • 13
  • 358
  • 360
dor4
  • 3
  • 1

1 Answers1

1

request.params() returns the HTTP parameters passed in the query string. In your case, there are none. Try with request.content() instead.

String json;
try{
    json = mapper.writeValueAsString(request.content());
} catch (Exception exp){ 
    json ="not found";
}
channel.sendResponse(new BytesRestResponse(OK,json));
Val
  • 207,596
  • 13
  • 358
  • 360