IllegalStateException on request.JSON

I am not sure what I am missing here, but I have followed some examples on how to get a JSON object from a request. Unfortunately, I have not been able to get it to work.

Here is the relevant app code:

mappings {
  path("/devices/:id") {
  	action: [
      PUT: "go"

def go() {
    def data = request.JSON
    log.debug data

here is my test client code (important crap removed):

import requests, json

api_endpoint = ''

headers = {'Authorization': 'Bearer XXXXXXX'}

url = api_endpoint + '/devices/lamp'
data = {'command': 'on'}

r = requests.put(url, data=data, headers=headers )

here is the error I get:
java.lang.IllegalStateException: getReader() has already been called for this request

I am testing this in the simulator, which I am not sure if that could be the issue or not, but either way it is frustrating. I feel like I am missing something obvious, but at this point I am beating my head against a wall and thought I’d see if someone else can see something I’m not seeing.

1 Like

Same thing is happening to me. Anyone able to figure out what’s going on here?

Old thread… but I sure wish I knew what was going on.

In my mappings I have:

path("/:type/:id") {
    action: [
        GET: "_api_get",
        PUT: "_api_put"

And the put method starts like this:

def _api_put() { "request ${request}"
	def json = request.JSON

So there shouldn’t to my knowledge be anything executing on the PUT request path before my method. The goes through (showing for example physicalgraph.api.FilteredHttpServletRequestWrapper@1a4a771d), but any attempt to access request.JSON results in something like java.lang.IllegalStateException: getReader() has already been called for this request @ line 192

I assume some type of invisible Groovy magic is calling getReader earlier. But for the life of me I can’t see what it is. Very frustrating. Even more frustrating that of course this must work fine for other people. :slight_smile:

1 Like

I’m having the exact same problem. I found a way around it, but the problem is still annoying. I fixed it (on accident) by specifying the access_token in the URL instead of as a header.

curl -X PUT -H "Authorization: Bearer <access_token>" -d {"POST_OR_PUT":"data"}

Running that returns:

{"error":true,"type":"java.lang.IllegalStateException","message":"An unexpected error occurred."}

However, both of these work just fine:

curl -X PUT -H "Authorization: Bearer <access_token>" -d {"POST_OR_PUT":"data"}<access_token>

curl -X PUT -d {"POST_OR_PUT":"data"}<access_token>

So if you feel like passing your access token in as a parameter, then it should work. However that’s dumb and it should work without it.