# How to read the body (json data) of the request in a custom plugin

**URL:** <https://discuss.elastic.co/t/how-to-read-the-body-json-data-of-the-request-in-a-custom-plugin/279375>\
**Category:** Elasticsearch\
**Created:** [July 22, 2021, 12:00pm UTC](https://discuss.elastic.co/t/how-to-read-the-body-json-data-of-the-request-in-a-custom-plugin/279375 "2021-07-22T12:00:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowfox38](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shadowfox38](https://discuss.elastic.co/u/shadowfox38)\
**Post date:** [July 22, 2021, 12:00pm UTC](https://discuss.elastic.co/t/how-to-read-the-body-json-data-of-the-request-in-a-custom-plugin/279375/1 "2021-07-22T12:00:09Z")

</div>

I am trying to create a custom plugin which needs to read the body of the request (by body i mean json data that is sent along with the request)

so i should be able to read the json data of a request like

```auto
POST myendpoint
{
     "somefield": "somevalue",
     "anotherfield": "anothervalue"
}

```

i currently have created a catAction, but it shows this error when i try to send a body

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "request [POST /myendpoint] does not support having a body"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "request [POST /myendpoint] does not support having a body"
  },
  "status" : 400
}

```

how can i send and read the body?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 22, 2021, 12:33pm UTC](https://discuss.elastic.co/t/how-to-read-the-body-json-data-of-the-request-in-a-custom-plugin/279375/2 "2021-07-22T12:33:29Z")

</div>

Did you create a subclass of the [BaseRestHandler](https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/rest/BaseRestHandler.java)?

What's the code?

---

<div class="post-metadata">

**Author:** ![shadowfox38](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shadowfox38](https://discuss.elastic.co/u/shadowfox38)\
**Post date:** [July 22, 2021, 12:37pm UTC](https://discuss.elastic.co/t/how-to-read-the-body-json-data-of-the-request-in-a-custom-plugin/279375/3 "2021-07-22T12:37:17Z")

</div>

So my endpoint is called deepclean  
DeepCleanPlugin.java

```auto
public class DeepCleanPlugin extends Plugin implements ActionPlugin
{
    @Override
    public List<RestHandler> getRestHandlers(Settings settings,
                                             RestController restController,
                                             ClusterSettings clusterSettings,
                                             IndexScopedSettings indexScopedSettings,
                                             SettingsFilter settingsFilter,
                                             IndexNameExpressionResolver indexNameExpressionResolver,
                                             Supplier<DiscoveryNodes> nodesInCluster)
    {
        return Collections.singletonList((new DeepCleanAction()));
    }
}

```

DeepCleanAction.java

```auto
public class DeepCleanAction extends AbstractCatAction
{
    @Override
    protected RestChannelConsumer doCatRequest(RestRequest request, NodeClient client)
    {
        Table table = getTableWithHeader(request);
        String message = request.content().toString();
        table.startRow();
        table.addCell(message);
        table.endRow();
        table.startRow();
        table.addCell("this should be after message");
        table.endRow();
        return restChannel ->
        {
            try
            {
                restChannel.sendResponse(RestTable.buildResponse(table,restChannel));

            } catch (final Exception e)
            {
            restChannel.sendResponse(new BytesRestResponse(restChannel, e));
            }
        };
    }

```

Edit : after i changed message to be = request.content().toutf8String()  
i was able to see the send the body, and the message was the same as body, but in string

how do i convert this into an object?  
I know i can use json parsers, but i don't think that is a proper way.

I wanted to know how the \_snapshot or other apis handle the body?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 19, 2021, 12:39pm UTC](https://discuss.elastic.co/t/how-to-read-the-body-json-data-of-the-request-in-a-custom-plugin/279375/5 "2021-08-19T12:39:22Z")

</div>

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