IllegalStateException on request.JSON


(A.) #1

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 = 'https://graph-na02-useast1.api.smartthings.com/api/smartapps/installations/XXXXXXXX'

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.


#2

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


(Daniel) #3

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() {
	log.info "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 log.info 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:


(Chandler Newby) #4

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"} https://graph.api.smartthings.com/api/smartapps/installations/appidThing/Phone/endpoint

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"} https://graph.api.smartthings.com/api/smartapps/installations/appidThing/Mapping/endpoint?access_token=<access_token>

curl -X PUT -d {"POST_OR_PUT":"data"} https://graph.api.smartthings.com/api/smartapps/installations/appidThing/Mapping/endpoint?access_token=<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.