# How to access json body in plugin

**URL:** <https://discuss.elastic.co/t/how-to-access-json-body-in-plugin/127882>\
**Category:** Elasticsearch\
**Created:** [April 12, 2018, 11:35pm UTC](https://discuss.elastic.co/t/how-to-access-json-body-in-plugin/127882 "2018-04-12T23:35:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdeck51](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@sdeck51](https://discuss.elastic.co/u/sdeck51)\
**Post date:** [April 12, 2018, 11:35pm UTC](https://discuss.elastic.co/t/how-to-access-json-body-in-plugin/127882/1 "2018-04-12T23:35:42Z")

</div>

Hello,

I'm making an endpoint/plugin and am having trouble understanding how to access fields in POST bodies.

For example, in [RestTermVectorsAction](https://github.com/elastic/elasticsearch/blob/4918924fae600cb7d9c329c55c76c44e8320a3c2/server/src/main/java/org/elasticsearch/rest/action/document/RestTermVectorsAction.java) it looks like it uses request.params("fieldname") to access the field. I've tried doing this but it never works.

```
request.paramAsBoolean("offsets", termVectorsRequest.offsets())

```

Is this how it's supposed to work or is there something else that extracts fields in a body?

Thanks

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [April 13, 2018, 5:33am UTC](https://discuss.elastic.co/t/how-to-access-json-body-in-plugin/127882/2 "2018-04-13T05:33:18Z")

</div>

The `request.param("foo")` method looks at HTTP parameters (as well as the named elements from the HTTP path). In the example class you mentioned, `RestTermVectorsAction`, you can see how the paths that class handles are registered in the constructor, and that elements of the path have stuff like `{index}`, which would be available when processing the request with `request.param("index")`.

However, to parse the body of the request, you want `request.contentParser()`. Again, in the class you mentioned, the is the code that parses the body:

```auto
try (XContentParser parser = request.contentOrSourceParamParser()) {
    TermVectorsRequest.parseRequest(termVectorsRequest, parser);
}

```

You'll notice that a slightly different variant of `contentParser()` is called there, but that is likely not relevant in your case (it falls back to parsing json with an HTTP `source` parameter passed within the URL).

---

<div class="post-metadata">

**Author:** ![sdeck51](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@sdeck51](https://discuss.elastic.co/u/sdeck51)\
**Post date:** [April 13, 2018, 3:25pm UTC](https://discuss.elastic.co/t/how-to-access-json-body-in-plugin/127882/3 "2018-04-13T15:25:02Z")

</div>

Thank you! I checked out the parseRequest method in TermVectorsRequest to get a better idea at what it was doing and was able to successfully read in the json fields. This can be closed!

---

<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:** [May 11, 2018, 3:25pm UTC](https://discuss.elastic.co/t/how-to-access-json-body-in-plugin/127882/4 "2018-05-11T15:25:15Z")

</div>

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