MattGrommes
MattGrommes

Reputation: 12354

request.JSON empty in Grails

I've got an Angular frontend sending a JSON update to my server but Grails won't see it in the controller. I just upgraded to Grails 2.5. I have a controller method on my EpisodeController called '/addGuest':

def addGuest(Episode episode) {
    println "J: "+ request.JSON;
}

and I'm sending the following from Angular

    service.addGuest = function(episode, guest) {
        return $http.put(getUrlForId(episode.id)+"/addGuest", {guest: guest});
    }

The guest json looks like

{guest: {name: "a", email: "a"}}

My route is

"/episode/$id/addGuest"(method: 'PUT', controller: "episode", action: "addGuest", parseRequest: true)

But the request.JSON is always blank "[:]" when I send in the data, either from Angular or from the Rest Console chrome plugin. If I change the Content-type in RestConsole from 'application/json' to 'text/plain', the data shows up! Having data come in via json seems like a basic thing that should just work so I'm sure I've got something messed up. Any help would be greatly appreciated. Thanks.

Upvotes: 2

Views: 1528

Answers (1)

Gregg
Gregg

Reputation: 35864

I had a similar issue a while back and racking my brain on the problem, I think once grails populates your Episode object, the request.JSON is emptied. The buffer is used up. You might remove Episode as an argument to the method, and see if request.JSON is available before any other processing happens.

def addGuest() {
    println "J: "+ request.JSON;
}

And BTW, if you need to send more data via JSON than what goes into the Domain, use a Command Object, then copy what you need to the appropriate domain(s). That way, you can still let grails do its convention stuff.

Upvotes: 6

Related Questions