How to access json body in plugin


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

For example, in RestTermVectorsAction 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?


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:

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

1 Like

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!

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