简体   繁体   中英

ElasticSearch custom plugin unable to get from post request parameters

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM